Design Algorithm for Longest Increasing Subsequence Length
Company: Experian
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithm design and data structure skills, covering subsequence analysis, constraint/graph modeling for seating, and external-memory sorting considerations for scalability.
Constraints
- 0 <= len(nums) <= 2500
- -10^4 <= nums[i] <= 10^4
- The subsequence must be strictly increasing (duplicates do not extend it).
Examples
Input: ([10, 9, 2, 5, 3, 7, 101, 18],)
Expected Output: 4
Explanation: [2, 3, 7, 18] is one longest strictly increasing subsequence of length 4.
Input: ([0, 1, 0, 3, 2, 3],)
Expected Output: 4
Explanation: [0, 1, 2, 3] is a longest increasing subsequence.
Input: ([7, 7, 7, 7, 7, 7, 7],)
Expected Output: 1
Explanation: All elements equal; strict increase allows only length 1.
Input: ([],)
Expected Output: 0
Explanation: Empty array has no subsequence; length 0.
Input: ([5],)
Expected Output: 1
Explanation: A single element is itself an increasing subsequence of length 1.
Input: ([5, 4, 3, 2, 1],)
Expected Output: 1
Explanation: Strictly decreasing; the best increasing subsequence is a single element.
Input: ([1, 2, 3, 4, 5],)
Expected Output: 5
Explanation: Already strictly increasing; the whole array qualifies.
Input: ([-2, -1, 0, -3, 4, -5, 6],)
Expected Output: 5
Explanation: [-2, -1, 0, 4, 6] is a longest increasing subsequence of length 5.
Input: ([3, 10, 2, 1, 20],)
Expected Output: 3
Explanation: [3, 10, 20] (or [2, 20] etc.) gives a maximum length of 3.
Input: ([2, 2, 2, 1, 3, 4],)
Expected Output: 3
Explanation: [2, 3, 4] is a longest strictly increasing subsequence of length 3.
Hints
- A patience-sorting approach maintains a list `tails`, where tails[k] is the smallest possible tail value of any increasing subsequence of length k+1.
- For each element x, binary-search for the leftmost position in `tails` whose value is >= x; replace it (or append x if x is larger than all tails). The answer is len(tails).
- Use bisect_left (not bisect_right) so that equal values overwrite rather than extend, enforcing STRICT increase.