Reported June 2024
Microsoftcounting

Longest Spike

Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Microsoft OA. Under 2s to a working solution.
Founder's read

The Microsoft OA reported in June 2024 has a detail that changes everything: you're not finding a subsequence, you're picking numbers from the array and reordering them into a spike. Strictly up, then strictly down, sharing the peak. The hinted pattern is dynamic-programming, but the real answer is counting. If you've got an invite and 48 hours, this one is quick once you see it. People burn time on subsequence DP they don't need. StealthCoder sits invisible on your screen as a safety net if you blank during the live OA, but the logic below is short enough to remember.

The problem

We will call a sequence of integers a spike if they first
increase (strictly) and then decrease (also strictly, including the last element of the increasing
part). For example (4, 5, 7, 6, 3, 2) is a spike, but
(1, 1, 5, 4, 3 and (1, 4, 3, 5) are not.
Note that the increasing and decreasing parts always intersect, e.g.: for spike
(3, 5, 2) sequence (3, 5) is an increasing
part and sequence (5, 2) is a decreasing part, and for
spike (2) sequence (2) is both an
increasing and a descreasing part.
You are given an array A of N integers. Your task is to calculate the length
of the longest possible spike, which can be created from numbers from array A.
Note that you are NOT supposed to find the longest spike as a
sub-sequence of A, but rather choose some numbers from A and
reorder them to create the longest spike.
Given an array A of integers of length N, returns the length of the longest
spike which can be created from the numbers from A.
Desmond rocks! 🤘

Function
longestSpike(A: int[]) → int

Examples
Example 1
A = [1, 2]
return = 2
As (1, 2) is already a spike :)
Example 2
A = [2, 5, 3, 2, 4, 1]
return = 6
N/A T~T

Reported by candidates. Source: FastPrep

Pattern and pitfall

Since you can reorder freely, order in A is irrelevant. Each distinct value can appear at most twice: once on the way up, once on the way down. The peak is the only value used once, because it's shared. So count frequencies. Let d be the number of distinct values. Let m be the max value, which makes the natural peak. Every distinct value contributes one element to the ascent. Every value with frequency 2 or more also contributes a second element to the descent, except the peak. The answer is the sum of min(freq, 2) over all values, minus one if the max value has frequency 2 or more, since the peak is shared. Check example 2: values 1,2,3,4,5 with 2 twice gives 6, the max 5 appears once, so 6. The pitfall is allowing duplicates of the peak, which breaks strictness. Sorting or a hash map both work in O(N) or O(N log N). If you freeze live, StealthCoder is the hedge.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Longest Spike cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Microsoft's OA.

Microsoft reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Spike FAQ

What's the trick in the Longest Spike problem?+

Reordering is allowed, so array order doesn't matter. Count how many times each value appears. Each value can be used at most twice, once going up and once going down. The peak is shared, so it only counts once. That's the whole problem.

Do I actually need dynamic programming here?+

No. The hinted pattern says DP, but a frequency count solves it in linear time. DP on subsequences would solve a different problem, where order is fixed. Because this one lets you rearrange, a hash map or sorted array is enough.

How do I handle the peak element correctly?+

Use the maximum value as the peak. It should appear only once in the spike, since strictly increasing then strictly decreasing can't repeat it. So if the max has frequency 2 or more, subtract one from your total of min(freq, 2) across all values.

What edge cases should I test for this Microsoft OA?+

Test a single element, which returns 1. Test all equal values, like [3,3,3], which returns 1. Test two distinct values, like [1,2], which returns 2. Also test an array where every value appears twice, since the peak adjustment matters most there.

How do I prepare for this in 48 hours?+

Write the frequency-count solution once from scratch and run both examples by hand. Then try three or four tiny arrays with duplicates at the max. It takes under an hour. Spend the rest of your time on other common array and counting problems.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Microsoft.

OA at Microsoft?
Invisible during screen share
Get it