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?
  1. A. 5 1
  2. B. 4 3
  3. C. 4 1
  4. 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

Exams that ask this topic