Minimum Operations for a Stepwise Sequence
Reported by candidates from Meta's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Meta reportedly served this one in July 2025, and the trap is sitting in the first line: you can only increase heights, never decrease. That kills the instinct to pick a median or the average and shift everything toward it. It's an array problem where you need a fixed target sequence, then you count the cost to reach it. If you blank under the timer, StealthCoder runs invisibly on screen and hands you the solution as a safety net. Know the trick before the invite timer starts anyway.
The problem
You are given an integer array structures, where structures[i] is the height of the ith structure in a row. In one operation, you may increase the height of any one structure by exactly 1. You cannot decrease a height. Transform the array into either of these stepwise patterns: Ascending: each height is exactly 1 greater than the height immediately before it. Descending: each height is exactly 1 less than the height immediately before it. Return the minimum number of operations required to form either pattern. Function solution(structures: int[]) → long Examples Example 1 structures = [1,4,3,2] return = 4 Add 4 units to the first structure. The final heights are [5,4,3,2], which form a descending stepwise pattern. Therefore, the minimum number of operations is 4. Example 2 structures = [5,7,9,4,11] return = 9 Add 2 units to the first structure, 1 unit to the second structure, and 6 units to the fourth structure. The final heights are [7,8,9,10,11], which form an ascending stepwise pattern. The total is 2 + 1 + 6 = 9 operations.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Both patterns are fully determined by one number, the starting height. For ascending, structure i must end at s + i. Each element needs s + i >= structures[i], so s must be at least max(structures[i] - i). Cost grows with s, so pick the smallest valid s. Descending works the same way with s - i, so s must be at least max(structures[i] + i). Then sum (target - original) for each pattern and return the smaller. That's O(n). The pitfall is the edge case: a naive solution anchors on structures[0] and ignores that a later tall structure forces the whole sequence up. Example 1 shows it, since the 4 in position 1 drags the descending start to 5. Use a 64-bit sum, because the return type is long. If you freeze during the live OA, StealthCoder is the hedge that surfaces this max-offset approach.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Minimum Operations for a Stepwise Sequence 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Meta's OA.
Meta reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Operations for a Stepwise Sequence FAQ
What's the trick in this Meta OA problem?+
Each pattern is locked once you choose the first value. Since you can only increase, the start must satisfy every element, so take the max of (height - index) for ascending or (height + index) for descending. Then sum the gaps. Compare both totals and return the smaller.
Why can't I just use the first element as the start?+
Because a later element can be too tall for that start. Example 1 shows it: descending from 1 fails because the 4 needs a higher ceiling, so the start becomes 5. Anchoring on index 0 undercounts or produces an invalid sequence.
How hard is this really?+
Easy to medium. The code is a single pass or two, but the insight about minimizing the start value is where people slip. Once you see that cost only increases with the start, it's a few lines.
Do I need a long for the answer?+
Yes. The function returns long, and with many structures and large heights the sum of increments overflows a 32-bit int. Accumulate in a 64-bit variable from the start and cast nothing down.
How do I prepare in 48 hours?+
Work a few array problems where a target sequence is derived from one parameter, and practice the max-of-offsets move. Hand-trace both examples from this question. Check empty and single-element arrays, since both patterns cost zero there.