Unequal Block Structure
Reported by candidates from WeRide's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The whole solution for this WeRide OA, reported in August 2026, fits in a three-slot array. Blocks, heights, per-unit costs, no two neighbors equal. It looks like it needs a heap or a map, but it doesn't. It's a rolling DP with three states per block, and with n up to 2 million you can't afford anything heavier. If you've got the OA invite and 48 hours, this is the pattern to lock in. Read the constraints twice, because the long return type matters. StealthCoder is there as a quiet safety net during the live assessment if you blank on the state definition, but the idea is simple enough to hold in your head.
The problem
You are given two arrays, heights and cost, of equal length. You may increase the height of block i by any nonnegative integer number of units. Each unit added to that block costs cost[i]. Choose the increases so that no two adjacent blocks have equal final heights. Return the minimum possible total cost. Function getMinCost(heights: int[], cost: int[]) → long Examples Example 1 heights = [2, 2, 3] cost = [4, 1, 5] return = 2 Increase the second block by 2 units. The final heights become [2, 4, 3], and the total cost is 2 * 1 = 2. Constraints 1 <= heights.length = cost.length <= 2 * 10^6 1 <= heights[i], cost[i] <= 10^9
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that you never need to raise a block by more than 2 units. A block has at most two neighbors, so at most two heights are forbidden for it. Trying increments 0, 1 and 2 always leaves a valid choice. Define dp[i][k] as the minimum cost for the first i blocks when block i is raised by k. For each k, take the minimum over previous increments j where heights[i-1]+j differs from heights[i]+k, then add k*cost[i]. Keep only the previous row, so memory is O(1) and time is O(n). Pitfalls: using int for the sum, since costs reach 10^9 times 2 times 2 million. Also forgetting the equality check compares final heights, not increments. Seed the first block with k*cost[0]. If the live OA freezes you on the transition, StealthCoder can surface the three-state recurrence fast.
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 Unequal Block Structure 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 WeRide's OA.
WeRide 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.
Unequal Block Structure FAQ
How hard is the WeRide Unequal Block Structure problem really?+
Medium. The code is about fifteen lines once you see it. The hard part is realizing increments of 0, 1 and 2 are enough. After that it's a standard three-state DP with a rolling row. Most people lose time looking for a greedy or heap approach.
What's the core trick?+
Each block has two neighbors, so at most two final heights are bad for it. That means you only ever need to try adding 0, 1 or 2 units. Anything beyond 2 costs more and never helps. That caps the state space at three per block.
Why can't I just use a greedy approach?+
Raising a block to dodge its left neighbor can collide with its right neighbor, and the cheapest fix may be to raise the neighbor instead. Local choices don't capture that cost trade-off. The DP compares all three options per block and keeps the global minimum.
What constraints should I watch with n up to 2 million?+
Use O(n) time and O(1) extra memory with three rolling variables. Use 64-bit integers for the total, since the cost sums overflow 32 bits. Read input once and avoid building a full dp table of objects or arrays per index.
How do I prepare for this in 48 hours?+
Write the three-state DP from scratch twice. Test it on the sample [2,2,3] with costs [4,1,5], which should return 2. Then try all-equal heights and a single block. Check overflow with max values. That covers nearly every edge case this problem has.