Data Structures & AlgorithmsHardProduct Company SDE · DP with binary search
Problem: In the O(n log n) longest increasing subsequence algorithm, the array tails[k] is maintained over the input. What does tails[k] represent?
- A. The largest last value of an increasing subsequence of length k + 1
- B. The smallest possible last value of an increasing subsequence of length k + 1
- C. The sum of the lengths of all increasing subsequences
- D. The index of the k-th element of the input
Correct answer: B. The smallest possible last value of an increasing subsequence of length k + 1
Keeping tails sorted lets each new value replace the first tail that is at least as large via binary search, so the list stays sorted and its length is the LIS length.
More Coding Interview Problems questions
- Problem: A dynamic array doubles its capacity whenever it is full and copies all elements. Which statement about n pushes is correct?
- Problem: How many ways are there to tile a 2 x 10 board completely with 2 x 1 dominoes (placed horizontally or vertically)?
- Array a = [1, -1, 2, 3, -2, 4, -3, 1]. How many contiguous non-empty subarrays have sum exactly 3? Solve it with running prefix sums and a…
- For a = [3, 4, -7, 2, -2, 5, -5, 1, 6], what is the length of the longest contiguous subarray whose elements sum to 0?
- Array a = [-2, 3, -4, 6, -1, 2]. How many non-empty subarrays have a sum divisible by 5? Count prefix sums by their remainder, normalised…
- In the binary string s = "1100110100", count the non-empty substrings that contain exactly as many 0s as 1s.