Reported August 2026
TikTokmath

Minimum Operations for Stepwise Structures

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

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

This TikTok OA from August 2026 looks like a fiddly array problem, but it collapses into one line of math. You can only raise heights, and the target is a strict staircase, ascending or descending. So the whole question is: what's the starting height of the staircase? If you've got an invite and 48 hours, this is the one to understand cold. If you blank during the actual assessment, StealthCoder runs invisibly on your desktop as a safety net and hands you the approach.

The problem

You are given an integer array structures, where structures[i] is the height of the structure at position i.
In one operation, you may increase the height of any one structure by exactly 1. You may perform as many operations as needed, but you may not decrease any height.
Transform the array into either of these stepwise patterns:
Ascending: every structure is exactly 1 unit taller than the structure immediately before it.
Descending: every structure is exactly 1 unit shorter than the structure immediately before it.
Return the minimum number of operations needed to obtain either pattern.

Function
minimumStepwiseOperations(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.
Example 2
structures = [5,7,9,4,11]
return = 9
Add 2 units to the first structure, 1 unit to the second, and 6 units to the fourth. The final heights are [7,8,9,10,11], which form an ascending stepwise pattern, using 9 operations.

Constraints
structures is an array of integers representing structure heights.
An operation increases exactly one structure height by 1.
The final adjacent heights must differ by exactly 1 throughout the array.
The source shows a Java execution time limit of 3 seconds and a memory limit of 1 GB.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Here's the reduction. For an ascending target, the final height at index i is base + i. You can only increase, so base + i >= structures[i] for every i. The smallest valid base is max(structures[i] - i). Cost is the sum of (base + i - structures[i]). For descending, the target is base - i, so you need base >= structures[i] + i. Take base = max(structures[i] + i) and sum the gaps. Return the smaller of the two totals. It's O(n) with two passes. The pitfall is overflow, since the return type is long, so accumulate in a long. Another trap is trying to pick a base by simulation or binary search. The max formula is exact, because a larger base only adds cost. StealthCoder is the hedge if the formula slips your mind live.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Minimum Operations for Stepwise Structures 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

TikTok reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Operations for Stepwise Structures FAQ

What's the trick in the TikTok Minimum Operations for Stepwise Structures problem?+

Fix the staircase by its first value. Since you can only increase heights, the base must cover every element. Ascending needs base = max(structures[i] - i). Descending needs base = max(structures[i] + i). Then sum the differences to the target and take the smaller total.

How hard is this OA question really?+

Easier than it looks. There's no DP and no search. Once you see that the target is determined by one number, it's two linear passes. The difficulty is spotting that, and not overthinking operations one at a time.

Why does the answer need a long?+

Heights and array length can push the total operations past 32-bit range. You're summing many gaps, each possibly large. Use a long accumulator and compute the base and gaps as long values too, so nothing overflows mid-calculation.

Do I need to check both ascending and descending?+

Yes. Example 1 shows it. [1,4,3,2] is cheapest as a descending staircase, costing 4. Compute both totals independently and return the minimum. Checking only one direction fails cases where the array already trends the other way.

How do I prepare for this in 48 hours?+

Work out the two examples by hand using the max formula. Then code it in one function with two loops, and test a single-element array and an already-perfect staircase. That's about 30 minutes. Skip broader review of unrelated topics.

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

OA at TikTok?
Invisible during screen share
Get it