MEDIUMasked at 2 companies

Construct Smallest Number From DI String

A medium-tier problem at 86% community acceptance, tagged with String, Backtracking, Stack. Reported in interviews at josh technology and 1 others.

Founder's read

You've got a string of 'D' and 'I' characters, and you need to construct the smallest number using digits 1-9 where each digit appears exactly once. The D means decreasing, I means increasing. It sounds simple until you realize the greedy approach fails. You can't just pick the smallest available digit at each step. This problem appears in online assessments at Goldman Sachs and Josh Technology. The acceptance rate sits at 86%, which means most people solve it, but the ones who don't usually hit a wall on the pattern. If this problem lands in your live OA and you blank on the trick, StealthCoder solves it in seconds, invisible to the proctor.

Companies asking
2
Difficulty
MEDIUM
Acceptance
86%

Companies that ask "Construct Smallest Number From DI String"

If this hits your live OA

Construct Smallest Number From DI String 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. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too.

Get StealthCoder
What this means

The trap is thinking greedy works here. You can't build left to right picking the smallest digit every time because a 'D' forces a decrease, and if you've already used small digits, you're stuck. The working approach uses a stack-based greedy method with backtracking logic. You iterate through the string, push digits onto a stack respecting the 'I' and 'D' constraints, then pop them in the right order to construct the answer. When you hit an 'I', you know the previous number must be smaller than the next one, so you pop until the constraint is satisfied. The final step is popping everything remaining. This isn't obvious without seeing the pattern, which is why it trips people up in real time. StealthCoder is the hedge for the one problem you didn't drill. It handles the stack manipulation and order of operations automatically, surfacing a working solution when the greedy intuition fails.

Pattern tags

The honest play

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

Construct Smallest Number From DI String recycles across companies for a reason. It's medium-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. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Construct Smallest Number From DI String interview FAQ

Why doesn't a simple greedy approach work?+

Greedy fails because committing to the smallest digit early forces you into dead ends later. A 'D' constraint needs the next digit to be smaller, but if you've already used small numbers, you can't satisfy it. The stack approach delays commitment, ensuring all constraints are satisfied globally, not just locally.

Is this still asked at Goldman Sachs and Josh Technology?+

Yes, both companies appear in the report for this problem. At 86% acceptance, it's a medium-difficulty screening problem. It tests your ability to recognize when greedy fails and think about constraint satisfaction, which is valuable for backend and systems roles.

What's the key insight behind the stack solution?+

The stack lets you build a prefix of the answer while deferring decisions about order. When you encounter 'I', you pop the stack because you know the previous segment must be strictly increasing. This respects all constraints without backtracking or trying multiple orderings.

How does this relate to Backtracking?+

The stack method is greedy with built-in constraint validation, so it doesn't need explicit backtracking. However, some solutions use backtracking to try different digit orderings. The stack approach is faster because it constructs the answer deterministically without exploring invalid branches.

What's the time complexity and does it matter for the OA?+

The stack approach is O(n) where n is the length of the input string. Since you're using digits 1-9, the input is small enough that even O(n^2) would pass. What matters is recognizing the pattern. Once you do, coding it takes five minutes.

Want the actual problem statement? View "Construct Smallest Number From DI String" 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.