Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA
Problem: Find the longest substring that appears at least twice in a string of length 10⁵. Which approach is efficient?
- A. Compare all pairs of substrings – O(n³)
- B. Sliding window with a frequency map of characters
- C. Binary search on the length L plus rolling hashes of all length-L substrings – O(n log n) expected (or a suffix array with LCP)
- D. KMP of the string against itself – O(n)
Correct answer: C. Binary search on the length L plus rolling hashes of all length-L substrings – O(n log n) expected (or a suffix array with LCP)
If a repeated substring of length L exists, one of every shorter length exists too, so L is monotonic and binary-searchable; checking a length is O(n) with rolling hashes. A suffix array plus LCP array gives the answer as the maximum LCP.
More Coding Interview Problems questions
- Problem: A recursive solution for 'number of ways to partition n into parts' is memoised on (n, largest part allowed). With n ≤ 1000, how…
- Problem: A function has T(n) = T(n − 1) + T(n − 2) + O(1) without memoisation. What is its growth?
- Problem: Dijkstra is run with a binary heap but WITHOUT the 'skip stale entry' check (if d > dist[u]: continue). What happens?
- Problem: n ≤ 10⁵ points on a line; answer 'how many points lie in [l, r]?' for 10⁵ queries. What is the simplest efficient solution?
- Problem: Implement a queue using two stacks and report the dequeued values. python class Q: def init(self): self.inb, self.out = [], []…
- Problem: Evaluate a basic calculator expression with +, −, parentheses and spaces. python def calc(s): res, num, sign, st = 0, 0, 1, []…