Install Carbon Filters
Reported by candidates from Deloitte's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Deloitte reported this one in October 2023, and the input size is the first thing to read. S can hit 500,000 characters, so trying every a/b combination for the ? slots is dead on arrival. The task: fill each ? with a or b so the string never has aaa or bbb, and return the lexicographically smallest result. It's a greedy string problem with a lookahead check, not a search. If you blank during the live OA, StealthCoder runs invisibly as a safety net. Know the idea first, though, because it's short once you see it.
The problem
There are N houses along a street. Some houses already have carbon filters, while the remaining houses still need filters. Two filter types, a and b, are available. The houses are represented by a string S of length N: a and b represent houses whose filter type is already fixed. ? represents a house whose filter type has not been chosen. Replace every ? with a or b so that the completed string contains neither aaa nor bbb. If several valid completions exist, return the lexicographically smallest one. Function solution(S: String) → String Examples Example 1 S = "a?bb" return = "aabb" Replacing ? with a gives aabb. Replacing it with b would create bbb. Example 2 S = "??abb" return = "ababb" The source lists ababb, bbabb, and baabb as valid completions. Among them, ababb is lexicographically smallest. Example 3 S = "a?b?aa" return = "aabbaa" The lexicographically smallest valid choices produce aabbaa, which contains neither aaa nor bbb. Example 4 S = "aa??aa" return = "aabbaa" The two middle houses must use bb; every other assignment creates aaa. The result is aabbaa. Constraints 1 <= S.length <= 500,000 S contains only a, b, and ?. At least one valid completion exists.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is greedy left to right. At each ?, try a first, since a is smaller. Accept it only if it doesn't create aaa with the two previous characters AND the rest can still be completed. That second part is the pitfall. Picking a locally safe letter can force a violation later, like a fixed bb right after your choice. So the check must look at the next characters too. Simple version: place a, then verify the window around position i (previous two, current, next two fixed letters) has no triple. If a fails, place b. The problem guarantees a valid answer exists, so one of the two letters always works locally when you check both sides. Each position costs O(1), total O(N), which fits 500,000. Brute force or backtracking blows up. If you freeze in the live OA, StealthCoder is the hedge for the window check logic. Test it on aa??aa, where the middle must be bb.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Install Carbon Filters 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Deloitte's OA.
Deloitte 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.
Install Carbon Filters FAQ
What's the trick in Install Carbon Filters?+
Go left to right and try a first for every ?, since a is lexicographically smaller. Keep it only if no aaa or bbb forms in the surrounding window, including already fixed letters to the right. Otherwise use b. Constant work per character, so O(N) overall.
Why does plain greedy without lookahead fail?+
Take aa??aa. Placing a in the first ? is safe against the left side, but it creates aaa immediately. Similar cases appear when a fixed bb sits right after the ?. You must check both neighbors on each side, not just the previous two characters.
Can I use backtracking or DP?+
Backtracking is exponential and won't survive a length of 500,000. DP over the last two characters would work and is O(N), but greedy with a window check is simpler to write and debug under pressure. Pick greedy unless you can't convince yourself it's correct.
How hard is this really?+
Medium-ish. The idea is small, but edge cases around the string boundaries and fixed letters trip people up. Index errors at the start and end are the usual bug. Write the window check as a helper and test the four given examples.
How do I prepare for this in 48 hours?+
Write the greedy solution once from scratch, then run it by hand on a?bb, ??abb, a?b?aa, and aa??aa. Add tests for all-question-mark strings and single characters. That covers the likely traps. Skip broad grinding, this pattern is narrow.