MEDIUMasked at 1 company

Lexicographical Numbers

A medium-tier problem at 76% community acceptance, tagged with Depth-First Search, Trie. Reported in interviews at Barclays and 0 others.

Founder's read

Lexicographical Numbers asks you to generate integers from 1 to n in lexicographical order without actually sorting them. That constraint kills the naive approach fast. You can't just build an array and sort; you need to traverse the number space like a tree. The problem appears in Barclays assessments and trips up candidates who don't spot the DFS pattern. If you hit this during your OA and the tree structure doesn't click immediately, StealthCoder runs invisibly and surfaces the solution in seconds while the proctor sees nothing.

Companies asking
1
Difficulty
MEDIUM
Acceptance
76%

Companies that ask "Lexicographical Numbers"

If this hits your live OA

Lexicographical Numbers 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 trick is treating numbers as a trie or prefix tree. The number 1 is the root. Its children are 10, 11, 12, and so on up to 19. You do depth-first search through this implicit tree, going as deep as possible before backtracking. Build the result by visiting nodes in lexicographical order without ever constructing the tree itself. Many candidates try to manually sort or compare strings, which is slow and brittle. The real win is recognizing that 'next number' in lexicographical order means either multiply by 10 (go deeper) or increment and reset children (backtrack). This is a DFS and Trie topic fused into one. When you see 'lexicographical' and 'without sorting' together, tree traversal is the hedge against blanking on the live assessment.

Pattern tags

The honest play

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

Lexicographical Numbers 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. 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.

Lexicographical Numbers interview FAQ

Is Lexicographical Numbers really asked at big tech companies?+

It's reported by Barclays. The acceptance rate is 76%, so it's not a rare edge case. It's medium difficulty, so you're expected to know DFS and recognize the tree pattern. It's the kind of problem that separates candidates who can adapt basic algorithms to non-obvious structures.

What's the trick everyone misses?+

Treating the number space as a tree instead of trying to sort. Once you see 1 has children 10-19, 2 has 20-29, and so on, DFS becomes obvious. Without that mental model, you'll waste time on string comparisons or manual sorting. The tree insight is everything.

Can you brute-force this with a comparator?+

Technically yes, but it's slow and inelegant. Sorting or comparing strings of all numbers 1 to n is O(n log n) time. DFS is O(n) because you visit each number exactly once. On a live OA, the efficient solution signals you understand the structure, not just the brute force.

How does Trie connect to this problem?+

A Trie naturally stores lexicographical order by prefix. This problem mimics a Trie structure implicitly: the number 123 comes after 122 because they share a prefix '12'. DFS traversal of a trie-like number tree gives you lexicographical order for free, without building an actual trie node structure.

What languages work best for this?+

Python and Java are typical for assessments. The logic is language-agnostic: you're doing DFS with two operations per node, one to go deeper (multiply by 10) and one to backtrack (increment the current prefix). Any language that supports recursion or a stack will work fine.

Want the actual problem statement? View "Lexicographical Numbers" 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.