The Find the Longest Valid Obstacle Course at Each Position interview question asks you to process an array of obstacle heights. For each position , you need to find the length of the longest non-decreasing subsequence that ends at and includes the obstacle at . This is essentially asking for the "Longest Increasing Subsequence" length for every prefix of the array.
Morgan Stanley and other financial firms use this problem to test your knowledge of efficient subsequence searching. The naive LIS algorithm is too slow for large arrays. It evaluations your ability to implement the Binary Search interview pattern for LIS, which is a fundamental optimization in algorithmic competitive programming.
This is a variation of the Longest Increasing Subsequence (LIS) problem solved using Patience Sorting or Binary Search.
tails where tails[i] is the smallest tail of all non-decreasing subsequences of length i+1.tails that is strictly greater than using bisect_right or upper_bound.tails.Obstacles: [1, 2, 3, 2]
tails = [1]. Length = 1.tails = [1, 2]. Length = 2.tails = [1, 2, 3]. Length = 3.[1, 2, 3] finds 3.
tails = [1, 2, 2].[1, 2, 3, 3].bisect_left (strictly increasing) instead of bisect_right (non-decreasing).Understand the difference between "strictly increasing" and "non-decreasing" in LIS problems. It changes which binary search function you use (lower_bound vs upper_bound). This problem is a perfect exercise for the LIS algorithm.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Block Placement Queries | Hard | Solve | |
| Divide Chocolate | Hard | Solve | |
| Find Minimum in Rotated Sorted Array II | Hard | Solve | |
| Kth Smallest Product of Two Sorted Arrays | Hard | Solve | |
| Minimize Max Distance to Gas Station | Hard | Solve |