Data Structures & AlgorithmsVery hardICPC regional · Amazon SDE OA
For "dcbadcba", among its rotations, which starting index i (0-based, the rotation s[i:] + s[:i]) gives the lexicographically smallest string? Report the first such index.
- A. 4
- B. 5
- C. 3
- D. 0
Correct answer: C. 3
Compare all rotations; the smallest one starts at the minimal letter 'a' followed by the smallest continuation. Booth's algorithm reports the same index in linear time.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is the maximum nesting depth of parentheses in "(()(()))()"?
- How many distinct values does floor(n / 3) take as n ranges over 1 to 20 inclusive?
- What is the sum of an = n mod 7 for n from 1 to 20 inclusive?
- From [1, 2, 2, 3, 5, 6, 6], what is the maximum number of disjoint pairs where the two values differ by at most 1?
- How many integers from 1 to 64 inclusive have a binary representation (without leading zeros) that reads the same forwards and backwards?
- What is the minimum number of characters that must be inserted anywhere in "abcb" to make it a palindrome?