HARDasked at 2 companies

Build Array Where You Can Find The Maximum Exactly K Comparisons

A hard-tier problem at 67% community acceptance, tagged with Dynamic Programming, Prefix Sum. Reported in interviews at Dunzo and 1 others.

Founder's read

You're building an array of length n using integers from 1 to m, and you must hit exactly k comparisons when finding the max. Dunzo and Amdocs have asked this. It feels impossible at first because the constraint isn't about the array itself but about the process of finding its maximum. You can't brute force or greedy this. Most candidates hit a wall here because they try to construct the array without thinking about how max-finding creates the comparison count. The trick is dynamic programming: count valid arrays by tracking position, the current maximum value, and remaining comparisons. If you blank on the approach in your OA, StealthCoder reads the problem off your screen and surfaces the DP structure in seconds, invisible to the proctor.

Companies asking
2
Difficulty
HARD
Acceptance
67%

Companies that ask "Build Array Where You Can Find The Maximum Exactly K Comparisons"

If this hits your live OA

Build Array Where You Can Find The Maximum Exactly K Comparisons is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage.

Get StealthCoder
What this means

The core insight is that a new comparison happens whenever you encounter an element larger than the current running max. You need to count arrays where this happens exactly k times. This is a DP problem where state is (position in array, highest value seen so far, comparisons remaining). For each position, you place a value from 1 to m. If it exceeds the current max, you use a comparison. If it doesn't, the max stays the same. You also need a Prefix Sum optimization because the DP transitions involve summing large ranges of previous states, which would be slow otherwise. The combination of DP state transitions plus prefix sums to avoid timeout is what separates this from easier DP problems. Candidates often miss the prefix sum optimization or overthink how to handle the max value constraint. When you're live in the assessment and haven't drilled this exact pattern, StealthCoder handles both the DP shape and the optimization instantly.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Build Array Where You Can Find The Maximum Exactly K Comparisons recycles across companies for a reason. It's hard-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Build Array Where You Can Find The Maximum Exactly K Comparisons interview FAQ

Is this really a hard problem or does it just look hard?+

It's genuinely hard. The acceptance rate is roughly 67%, which is solid for hard-tier problems. The issue isn't math or complicated logic. It's recognizing that comparisons equal the number of times you see a value bigger than the running max, then building a DP to count arrays with exactly k such moments. Most people skip it or timeout without the prefix sum layer.

What's the most common mistake?+

Trying to enumerate or construct arrays directly instead of counting. The second mistake is building the DP but forgetting that transitions involve summing ranges of previous states, which forces you to add a prefix sum pass. Without that, your solution is correct but times out.

How does this relate to the Dynamic Programming and Prefix Sum topics?+

This problem is a textbook two-layer optimization. DP counts valid arrays by tracking state (position, max seen, comparisons left). Prefix Sum isn't a separate topic; it's the tool that makes DP feasible by collapsing O(m) range-sum transitions into O(1) lookups per state.

Do Dunzo and Amdocs really ask this exact problem?+

Both companies report this problem. It's not the most frequent ask, but it shows up in their senior backend and systems design loops. If you're in their funnel and see it, you can't skip it.

How do I practice this if there aren't many similar problems?+

Master the DP states first on a simpler variant (like counting arrays with a max value less than m). Then practice prefix sum optimization on other DP problems. This problem combines both well but isn't the only way to learn either concept.

Want the actual problem statement? View "Build Array Where You Can Find The Maximum Exactly K Comparisons" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.