Remove Duplicate Characters
Reported by candidates from NatWest's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
A hash set is the whole solution to this NatWest OA question, reported in July 2026. You get a string, you keep the first copy of each character, and you drop the rest while the original order stays intact. It's an easy problem, which means the only way to lose is to overthink it or fumble the details under a timer. If you've got the invite in your inbox, spend five minutes on the approach and you're done. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment, but this one is simple enough that you probably won't need it.
The problem
You are given a string s. Remove duplicate characters while preserving the order of their first occurrence, and return the resulting string. Function removeDuplicateCharacters(s: String) → String Examples Example 1 s = "banana" return = "ban" The first occurrences are b, a, and n. Later repeated characters are skipped. The source shared the rule but did not include this exact sample. FastPrep added this small example so the behavior can be checked directly.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a single left-to-right pass with a set of seen characters. For each character, check the set. If it's missing, add it and append it to the output. If it's there, skip it. That gives you O(n) time and O(k) space, where k is the alphabet size. The common pitfall is sorting or using a plain set to build the result, which loses first-occurrence order. Another is building the string with repeated concatenation in a language where that's slow, so use a list or builder and join at the end. Check edge cases too: an empty string, a string of one repeated character, and mixed case, where 'A' and 'a' are different unless the problem says otherwise. For "banana" the answer is "ban". If you blank on the live OA, StealthCoder is the hedge that hands you this loop in seconds.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Remove Duplicate Characters 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass NatWest's OA.
NatWest reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Remove Duplicate Characters FAQ
How hard is the NatWest Remove Duplicate Characters question really?+
It's easy. One pass, one set, one output buffer. The NatWest report from July 2026 describes a plain string task with no hidden constraints. Most candidates finish the logic in a few minutes. The risk is sloppy edge cases, not the algorithm.
What's the trick to keeping the original order?+
Iterate the string once from left to right and only append a character the first time you see it. Track seen characters in a hash set. Don't sort and don't convert the whole string to a set first, because that can scramble the order.
What time and space complexity should I state?+
Time is O(n) since you touch each character once with O(1) average set lookups. Space is O(k) for the set, where k is the number of distinct characters, plus O(n) for the output. If the alphabet is fixed, the set space is effectively constant.
Which edge cases should I test before submitting?+
Test an empty string, a single character, a string where every character is the same, and one with no duplicates. Also check case sensitivity. Unless the statement says otherwise, treat uppercase and lowercase as different characters. Run the banana example and confirm you get ban.
How do I prepare for this in 48 hours?+
Write the set-plus-builder loop from memory in your main language twice. Then do two or three related string and hash set problems, like first unique character or valid anagram, so the pattern feels automatic. You don't need more than an hour for this question.