Longest Substring with Even Occurrences
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 it looks like string counting until you see what it really reduces to: a bitmask of 26 parities and a hash map of first-seen positions. If you've got an invite in your inbox, this is the trick to lock in. Find the longest substring where every letter shows up an even number of times. N goes up to 100,000, so brute force dies fast. Once you see the prefix parity idea, the code is about ten lines. If you blank during the live OA, StealthCoder is the silent safety net running on your screen.
The problem
Write a function:
def solution(S)
that, given a string S consisting of N lowercase English letters, returns the length of the longest substring in which every letter occurs an even number of times. A substring is defined as a contiguous segment of a string. If no such substring exists, return 0.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..100,000];
string S consists only of lowercase letters ('a'-'z').
Function
solution(S: String) → int
Examples
Example 1
S = "bdaaadadb"
return = 6
Substrings in which every letter occurs an even number of times are "aa", "adad", "daaadad" and "aaadad". The length of the longest of them is 6.
Example 2
S = "abacb"
return = 0
There is no non-empty substring in which every letter occurs an even number of times.
Example 3
S = "zthtzh"
return = 6
Every letter in the whole string occurs an even number of times.Reported by candidates. Source: FastPrep
Pattern and pitfall
Track a 26-bit mask where bit i flips each time letter i appears. A substring has all-even counts exactly when the mask at its start equals the mask at its end. So store the first index where each mask appears, with mask 0 at index -1. Walk the string, flip the bit, and if the mask was seen before, the candidate length is i minus the first index. Otherwise record it. Answer is the max, or 0. The pitfall is forgetting to seed mask 0 at -1, which breaks cases like "zthtzh" where the whole string qualifies. Another trap is updating the map on repeat sightings, which shrinks your lengths. Only store the first occurrence. Time is O(N), space is at most 2^26 keys but really bounded by N. StealthCoder is the hedge if the bitmask idea won't come to you under the clock.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Longest Substring with Even Occurrences 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 would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Deloitte's OA.
Deloitte reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Substring with Even Occurrences FAQ
What's the trick for the Deloitte longest even-occurrence substring problem?+
Use prefix parity. Keep a bitmask where each bit tracks whether a letter has appeared an odd or even number of times so far. Two equal masks at different positions mean the substring between them has all-even counts. Store the first index of each mask and maximize the gap.
Is this really dynamic programming?+
Not in the classic table sense. It's closer to prefix sums plus a hash map, with bit manipulation for the state. The state is the parity mask, and you remember the earliest position for each. Don't waste time building a 2D DP grid, it won't fit N of 100,000.
Why do I need to seed the mask 0 at index -1?+
Because a substring starting at index 0 has no earlier prefix to match against. Seeding mask 0 at -1 lets the whole prefix count when its mask returns to 0. Without it, a string like "zthtzh" returns the wrong answer instead of 6.
What complexity should I aim for?+
O(N) time with a single pass and a hash map keyed by the mask. A nested loop checking every substring is O(N^2) or worse and will time out at 100,000 characters. Interviewers expect the linear prefix-mask solution here.
How do I prepare for this in 48 hours?+
Write the solution from scratch twice using the mask and first-seen map. Test with the three examples: "bdaaadadb" gives 6, "abacb" gives 0, "zthtzh" gives 6. Then try a single character and an all-same-letter string to check edge cases. That covers the common failure points.