Data Structures & AlgorithmsHardAmazon · Microsoft OA
Using O(n²) DP (dp[i] = 1 + max(dp[j]) over all j < i with a[j] < a[i]), what is the length of the longest STRICTLY increasing subsequence of [7, 21, 7, 2, 20, 9, 4, 1, 19]?
- A. 3
- B. 5
- C. 4
- D. 2
Correct answer: A. 3
Each dp[i] holds the length of the longest strictly increasing subsequence ending at index i, built from smaller dp values at earlier strictly-smaller elements; the overall answer is the maximum dp value, 3. Allowing equal values to extend the subsequence (non-decreasing, not strictly increasing) can give a different count, 4.
More Coding Interview Problems questions
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…
- Using a min-heap of size k (push each element, pop when size exceeds k, so the heap always holds the k largest seen so far), what is the k…
- Fractional knapsack, capacity 15 (items can be split). Items (weight, value): (7, 7), (5, 7), (3, 14), (5, 11), (6, 5). Using the greedy…
- What is the length of the longest common subsequence of "BEADCBBDA" and "BAAEDC"?
- A divide-and-conquer routine satisfies T(n) = 8T(n/2) + n². What is its running time (by the master theorem, extended for polylog terms…