Data Structures & AlgorithmsEasyAccenture · Cognizant coding round pattern
Problem: Is b a rotation of a? Check equal lengths and whether b occurs in a + a.
Problem: Is b a rotation of a? Check equal lengths and whether b occurs in a + a.
def is_rot(a, b):
return len(a) == len(b) and b in a + a
print(is_rot("cabbage", "bagecab"), is_rot("abcd", "acbd"), is_rot("aa", "aaa"))What is printed?- A. True False False
- B. True False True
- C. True True False
- D. False False False
Correct answer: A. True False False
Every rotation of a is a substring of a + a ("cabbagecabbage" contains "bagecab"). "acbd" is a permutation but not a rotation. Without the length check, "aaa" would be found inside "aaaa". O(n) with a linear-time substring search.
More Coding Interview Problems questions
- Problem: String t is string s shuffled with one extra letter added. Find the extra letter by XOR-ing all character codes. python from…
- Problem: Look-and-say: read the previous term aloud in runs ("1" → "11" → "21" …). python def say(s): out, i = [], 0 while i < len(s): j =…
- Problem: Write a string in a zigzag over r rows (down, then diagonally up) and read it row by row. python def zigzag(s, rows): if rows ==…
- Problem: Compare dotted version strings numerically part by part (missing parts count as 0); return 1, 0 or −1. python def cmpver(a, b): x…
- Problem: Multiply two non-negative integers given as decimal strings without converting them to int; digit i·j lands in position i + j + 1…
- Problem: Reverse the bit order of an 8-bit value (bit 0 ↔ bit 7, …). python def rev8(x): r = 0 for in range(8): r = (r << 1) | (x & 1); x…