What is the longest increasing subsequence (LIS)?
Why Interviewers Ask This
Candidates at the intermediate level are expected to not only know this concept but explain the trade-offs involved. Interviewers use this question to see if you can reason about design decisions, not just recall facts.
Answer
The Longest Increasing Subsequence (LIS) problem finds the length of the longest strictly increasing subsequence in an array. Example: in [10, 9, 2, 5, 3, 7, 101, 18], LIS = [2, 3, 7, 101], length 4. DP solution (O(n²)): dp[i] = LIS ending at index i. For each i, dp[i] = max(dp[j] + 1) for all j < i where arr[j] < arr[i]. Base: dp[i] = 1. Answer: max(dp[i]). Optimal O(n log n) solution using binary search: maintain a sorted array tails where tails[i] is the smallest possible tail element of all increasing subsequences of length i+1. For each element x: binary search (lower_bound) for the first element in tails ≥ x; if found, replace it with x; otherwise append x. The length of tails is the LIS length. This gives the LIS length but not the actual sequence (requires additional tracking). Applications: (1) Patience sorting (O(n log n) deck of cards sort related to LIS); (2) Dilworth's theorem — minimum number of non-increasing subsequences to partition the array = LIS length; (3) Edit distance variant; (4) Stock trading problems. Often combined with other DP problems (LIS in 2D — the Russian doll envelopes problem).
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Data Structures & Algorithms codebase.