Reported September 2026
Amazonstack

Remove Duplicate Letters for the Largest Result

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

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

A stack decides this one. The Amazon OA reported in September 2026 asks for the lexicographically largest subsequence with each distinct letter exactly once, and the whole solution hinges on a monotonic stack plus a last-occurrence map. It's the mirror of the classic smallest version, which means a lot of people will pattern-match, flip the wrong comparison, and fail hidden tests. You've got 100000 characters to handle, so brute force is dead. If you blank mid-assessment, StealthCoder runs invisibly as a safety net and reads the problem for you. Know the stack idea first.

The problem

Given a lowercase string s, remove characters so that every distinct letter appears exactly once. The remaining characters must preserve their original relative order.
Return the lexicographically largest possible result.

Function
largestUniqueLetters(s: String) → String

Examples
Example 1
s = "bcabc"
return = "cab"
The subsequence cab contains each distinct letter once and is lexicographically largest.
Example 2
s = "cbacdcbc"
return = "cbad"
Keeping the early c and b allows the largest valid prefix.
Example 3
s = "bbcaac"
return = "bca"
The best unique-letter subsequence is bca.

Constraints
1 ≤ s.length ≤ 100000.
s contains only lowercase English letters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Count or record the last index of each letter. Walk the string left to right with a stack and a visited set. If the current letter is already in the stack, skip it. Otherwise, while the stack top is smaller than the current letter and that top appears again later (its last index is greater than i), pop it and unmark it. Then push the current letter and mark it. The comparison is the flip from the smallest version: pop when top < current, not top > current. The common pitfall is popping a letter that never appears again, which drops it from the answer entirely. Another is forgetting the visited check, which creates duplicates. Check example 3, bbcaac: you get bca. Runs in O(n) time with at most 26 stack entries. If the logic slips under pressure, StealthCoder is the hedge during the live OA.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Remove Duplicate Letters for the Largest Result 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Remove Duplicate Letters for the Largest Result FAQ

What's the trick to this Amazon problem?+

Use a monotonic stack with a last-occurrence array. Pop the top when it's smaller than the current letter, and only if that top letter shows up again later. Skip letters already on the stack. That gives the largest result in one pass.

How is this different from Remove Duplicate Letters on LeetCode?+

The LeetCode version wants the smallest lexicographic result. This one wants the largest. The structure is identical, but the pop comparison flips. Pop while stack top is less than the current character, still guarded by the last-occurrence check.

What's the time and space complexity?+

Time is O(n), since each character is pushed and popped at most once. Space is O(1) in practice because only 26 lowercase letters can sit on the stack, plus a 26-size last-index array and visited set.

What edge cases should I test?+

Test a single character, a string with all the same letter like aaaa, an already descending string like cba, and an ascending one like abc. Also run example 3, bbcaac, to confirm you don't pop letters that have no later occurrence.

How do I prepare for this in 48 hours?+

Write the smallest-version solution from memory, then flip it to largest and trace all three examples by hand. Practice explaining why the last-occurrence check keeps every letter represented. Two or three clean runs on paper is enough for this pattern.

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

OA at Amazon?
Invisible during screen share
Get it