Reported September 2026
DRWgreedy

Largest Number With Even Digit Frequencies

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

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

DRW reported this one in September 2026, and it looks friendlier than it is. The string is only 1s and 2s, you delete characters, and both digit counts must end up even. The edge case that wrecks a naive solution is when both counts are odd, because now you owe two deletions and the order you pick them in matters. It's a greedy problem with a parity check on top, not a search. Once you see that, it's about fifteen lines. If the OA clock is ticking and your mind goes blank on which character to drop, StealthCoder runs invisibly on your screen and hands you the approach as a safety net.

The problem

You are given a string digits containing only the digits 1 and 2.
Delete zero or more characters without changing the relative order of the characters that remain. In the resulting string, both 1 and 2 must occur an even number of times; zero occurrences of a digit is allowed.
Return the numerically largest valid remaining string.

Function
solution(digits: String) → String

Examples
Example 1
digits = "121212"
return = "2121"
Each digit occurs three times, so one 1 and one 2 must be removed. Removing the first 1 and the last 2 produces 2121, the largest valid result.
Example 2
digits = "2121122"
return = "221122"
The string has three 1s and four 2s. Removing the first 1 makes both frequencies even and yields 221122.
Example 3
digits = "1111"
return = "1111"
The four 1s already have even frequency, so keeping every character gives the largest result.

Constraints
3 <= digits.length <= 200000.
digits contains only 1 and 2.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Count the 1s and 2s first. Both even: return the string untouched. Longer beats shorter numerically, so you delete the minimum. One odd count means one deletion. Both odd means exactly two, one 1 and one 2. Now maximize lexicographically. To drop a 1, remove the first 1 that sits directly before a 2. If no 1 is followed by a 2, drop the last 1. To drop a 2, remove the last 2, since removing an earlier one can push a smaller digit forward. The classic pitfall is deleting the first occurrence of each digit blindly, or trimming from the ends. Example 1, 121212, becomes 2121 only with the smart picks. Run it in one pass with O(n) time. With n up to 200000, avoid rebuilding strings repeatedly. StealthCoder is your hedge in the live OA if the both-odd case slips your mind under pressure.

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 Largest Number With Even Digit Frequencies 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 DRW's OA.

DRW 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.

Largest Number With Even Digit Frequencies FAQ

What's the trick in the DRW largest number with even digit frequencies problem?+

Delete as few characters as possible, because a longer string is always the larger number. Then pick which characters to delete so the result is lexicographically largest. The number of deletions is decided entirely by the parity of the 1 count and the 2 count.

What's the edge case that breaks most solutions?+

Both counts odd. You must delete one 1 and one 2, and a naive approach that handles only a single deletion fails. Also watch inputs like 1111 or all 2s where nothing needs removing, and strings where no 1 is followed by a 2.

Which 1 do I delete, and which 2?+

For a 1, delete the first one that's immediately followed by a 2, since that lifts a bigger digit forward. If none exists, delete the last 1. For a 2, delete the last 2 in the string. Earlier 2 removals can shift a smaller digit forward and hurt the result.

How hard is this really, and what's the complexity?+

Easy to medium. The logic is short but the reasoning is easy to get wrong. Count digits, decide deletions, find indices with one scan, and build the answer once. That's O(n) time, which matters with length up to 200000.

How do I prepare in 48 hours?+

Write this solution once from scratch and test it on the three examples plus a both-odd case you invent. Then practice the single-deletion lexicographic maximum pattern on other strings. Focus on proving why the greedy picks work, not on memorizing code.

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

OA at DRW?
Invisible during screen share
Get it