Find the length of the longest strictly increasing subsequence in O(n log n) with patience sorting and bisect, after the O(n²) DP. A must-know FAANG pattern.
The problem
Return the length of the longest strictly increasing subsequence of nums (elements need not be adjacent). Aim for O(n log n).
Examples
Example 1
Input
lis_length([10, 9, 2, 5, 3, 7, 101, 18])
Expected output
4
Example 2
Input
lis_length([0, 1, 0, 3, 2, 3])
Expected output
4
+ 4 hidden tests on Submit — strictly increasing.
Edge cases to ask about
- Empty
- All equal
- Strictly decreasing
Hints
0/3How an interviewer scores this
0/9Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.
Complexity Lab
What does this cost as n grows?
Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.
Pick both to reveal the answer.
Measure it
Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.
From brute force to optimal
The progression an interviewer wants to hear, one step at a time.
| Approach | Time | Space | Idea |
|---|---|---|---|
| DP: best ending at each i | O(n²) | O(n) | dp[i] = 1 + max(dp[j]) for j < i with nums[j] < nums[i]. |
| bestPatience sorting + binary search | O(n log n) | O(n) | Keep the smallest tail for each length. |
Walkthrough of the optimal approach (try it yourself first)
tails[i] is the smallest value that can end an increasing subsequence of length i + 1. The list is always sorted. For each x, binary-search the first tail ≥ x (bisect_left, so equal values don't extend a strict run): replace it, or append if x beats every tail. The answer is len(tails).
tails is not an actual subsequence — say that before the interviewer asks.
Complexity: O(n log n) time, O(n) space. Each element does one binary search over `tails`, which has at most n entries.
Reveal the reference solution
import bisect def lis_length(nums): tails = [] # tails[i] = smallest possible tail of an increasing run of length i + 1 for x in nums: i = bisect.bisect_left(tails, x) if i == len(tails): tails.append(x) else: tails[i] = x return len(tails)
The brute force, for comparison
def lis_length(nums): if not nums: return 0 dp = [1] * len(nums) for i in range(len(nums)): for j in range(i): if nums[j] < nums[i]: dp[i] = max(dp[i], dp[j] + 1) return max(dp)
Follow-ups interviewers ask
- Return the subsequence itself.
- Non-decreasing version (bisect_right).
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of Longest Increasing Subsequence in Python?
The optimal solution runs in O(n log n) time and O(n) auxiliary space. Each element does one binary search over tails, which has at most n entries.
What is the brute-force approach, and how do you optimise it?
DP: best ending at each i: O(n²) time, O(n) space. dp[i] = 1 + max(dp[j]) for j < i with nums[j] < nums[i]. Patience sorting + binary search: O(n log n) time, O(n) space. Keep the smallest tail for each length.
What follow-up questions do interviewers ask about Longest Increasing Subsequence?
Return the subsequence itself. Non-decreasing version (bisect_right).
