Data Structures & AlgorithmsModerateTCS NQT Digital · Product Company SDE
Problem: Length of the longest strictly increasing subsequence with the O(n²) DP.
Problem: Length of the longest strictly increasing subsequence with the O(n²) DP.
int lis(vector<int>& a) {
vector<int> dp(a.size(), 1);
for (int i = 0; i < a.size(); i++)
for (int j = 0; j < i; j++)
if (a[j] < a[i]) dp[i] = max(dp[i], dp[j] + 1);
return *max_element(dp.begin(), dp.end());
}
// lis({0, 8, 4, 12, 2, 10, 6, 14, 1, 9}) and lis({7, 7, 7})What are the two results?- A. 5 1
- B. 4 3
- C. 4 1
- D. 6 1
Correct answer: C. 4 1
dp[i] = longest increasing subsequence ending at i. Several chains reach length 4 – 0, 4, 10, 14 or 0, 8, 12, 14 or 0, 2, 6, 9 – and none reaches 5. Strictly increasing means equal values do not chain, so [7, 7, 7] → 1. O(n²).
More Coding Interview Problems questions
- Problem: A hash table keyed by short strings uses Java's String.hashCode. Why do some keys collide? java // String.hashCode: h = 31h + c…
- Problem: Binary search on large indices in C++ (int is 32-bit). cpp int lo = 2000000000, hi = 2100000000; int bad = (lo + hi) / 2; //…
- Problem: Find the celebrity (known by all, knows nobody) with O(n) calls to knows(a, b). python M = [[0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 0…
- Problem: Delete exactly one element from a binary array and return the longest run of 1s that can remain. python def afterdelete(a): l =…
- Problem: Find the minimum number of railway platforms with a sweep line over arrival and departure events (a train arriving when another…
- Problem: Find how many numbers in 1..n have all distinct digits — the digit-DP-free shortcut for n = 10ᵏ uses counting. python def…