DSA interview question
You're analyzing sensor data from a drone's altitude log during a single flight leg. A "clean" flight profile climbs to a peak altitude and then descends — this is called a bitonic pattern (strictly increasing, then strictly decreasing). Due to noise, the raw altitude readings aren't perfectly bitonic, but you want to find the longest subsequence of readings that does follow this climb-then-descend shape, to identify the cleanest usable segment of the flight for calibration purposes.
Given an integer array nums, find the length of the longest bitonic subsequence — a subsequence that is strictly increasing, then strictly decreasing (both parts non-empty is not required; a purely increasing or purely decreasing sequence also counts as bitonic).
Example: nums = [1,11,2,10,4,5,2,1] — the longest bitonic subsequence is [1,2,10,4,2,1] or [1,11,10,5,2,1], both of length 6.
int longestBitonicSubsequence(vector<int>& nums);
Input nums | Output | Explanation |
|---|---|---|
[1,11,2,10,4,5,2,1] | 6 | e.g. [1,2,10,4,2,1] |
[12,11,40,5,3,1] | 5 | Increasing part [12,40] (or [11,40]), then decreasing [40,5,3,1] |
[80,60,30,40,20,10] | 5 | Purely decreasing after single peak: [80,60,30,20,10] or similar |
[1,2,3,4,5] | 5 | Purely increasing — still valid bitonic |
[5,4,3,2,1] | 5 | Purely decreasing — still valid bitonic |
[1] | 1 | Single element |
[] | 0 | Empty array |
(Always confirm: strictly increasing/decreasing vs non-strict, and whether a monotonic array is a valid answer.)
nums? (Signals whether O(n²) is acceptable or O(n log n) is expected.)On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in