Build the longest spike from an array
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Microsoft. It tests arrays, hash map and greedy.
A spike is a sequence of integers that strictly increases up to a single peak and then strictly decreases. The peak belongs to both parts, and either part may be empty, so a single number is a spike of length 1.
For example, [1, 6, 9, 4] is a spike. [3, 3, 8, 2] is not, because the rising part repeats 3. [2, 9, 9, 1] is not, because the peak is not unique.
You are given an integer array arr of length n. You may choose any subset of its elements (each element used at most once) and arrange them in any order.
Implement longest_spike(arr: List[int]) -> int, which returns the maximum length of a spike you can build. Return 0 for an empty array.
Constraints
0 <= n <= 10^5-10^9 <= arr[i] <= 10^9- Aim for O(n) or O(n log n).
Example 1
arr = [2, 5, 3, 2, 8, 3, 3]
output: 6
One answer is [2, 3, 5, 8, 3, 2]. Only one 5 exists, so it goes on one side. One of the three 3s cannot be used.
Example 2
arr = [7, 7, 1, 1]
output: 3
The best spike is [1, 7, 1]. A second 7 cannot be placed anywhere.
Be ready for follow-ups: return the spike itself, require both sides to be non-empty, fix the peak value, or solve it without reordering (keep the original order).
Clarifying questions
- Can the answer have both sides empty (a single number)? The statement says yes.
- Is the answer only the length, or should the spike itself be returned too?
- Can values be negative or repeat many times? Both are allowed by the constraints.
- Does the empty array return 0?
What a strong answer covers
- Sees that order doesn't matter, so only counts do: each value can appear at most twice, once on each side, and the peak value only once.
- Picks the largest value as the peak, since every other value can then go on both sides. The answer is the sum of
min(count, 2)over all values, minus 1 if the largest value appears more than once. - Explains why a smaller peak can't do better: every value above it would be lost.
- O(n) time with a hash map, O(n) space.
- Tests: both examples, a single element, all equal values (length 1), all distinct values (length n), negative values, and a brute force over small arrays.
Follow-ups
- Return the spike itself: the values used once or twice in ascending order on the left, the peak, then the right side descending.
- Both sides must be non-empty.
- The peak value is fixed.
- Keep the original order: the longest bitonic subsequence, O(n log n) with two longest-increasing-subsequence passes.
Where it's asked
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
