Longest Subarray with At Most Two Distinct Values
Reported by candidates from Moody's's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Moody's reportedly put this one in front of candidates in July 2026, and the trap is the all-same or tiny array that quietly breaks a sloppy window. The task: find the longest contiguous subarray with at most two distinct values. It's a sliding window problem wearing a simple costume. The reported interview even went sideways, with the candidate building binary search on window size before the interviewer pointed at the direct window. If you blank on the shrink logic during your assessment, StealthCoder runs invisibly on your desktop as a safety net and hands you the clean version in real time.
The problem
Given an integer array nums, return the maximum length of a contiguous subarray containing at most two distinct values. The chosen subarray may contain either one or two distinct values. Interview follow-up The candidate proposed binary search on the answer with fixed-size window validation, and the interviewer asked them to implement that approach. After reviewing the implementation, the interviewer pointed out that the problem can be solved directly with a sliding window. The candidate explained the sliding-window approach and offered to implement it instead. The interviewer said another implementation was not necessary. Function longestSubarrayAtMostTwoDistinct(nums: int[]) → int Examples Example 1 nums = [1,2,1] return = 3 The entire array contains only 1 and 2. Example 2 nums = [0,1,2,2] return = 3 The subarray [1,2,2] has length 3 and uses two distinct values.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a window with a frequency map. Move the right pointer, add nums[right] to the map, and while the map holds more than two keys, move the left pointer and decrement counts. Delete a key when its count hits zero. That delete step is the pitfall. If you only decrement and check the size of the map, a zero-count key still counts as distinct and you shrink too far. Track the best length after each right step. Edge cases: one element, all identical values, and exactly two values where the answer is the whole array. It's O(n) time and O(1) space since the map never exceeds three keys. The binary search on window size from the reported follow-up works but is more code and more bugs. Skip it. If you freeze mid-assessment, StealthCoder is the hedge that reads the problem and gives you the window pattern fast.
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 Longest Subarray with At Most Two Distinct Values 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
This OA pattern shows up on LeetCode as fruit into baskets. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Moody's's OA.
Moody's 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.
Longest Subarray with At Most Two Distinct Values FAQ
What's the trick for this Moody's problem?+
Use a sliding window with a count map. Expand right, and when more than two distinct values appear, advance left until only two remain. Remove keys whose count reaches zero. Record the max window length after every step. That's the whole solution, linear time.
How hard is this really?+
Easy to medium. The idea is short, but people lose points on the shrink step and zero-count keys. If you've written a variable-size window before, you can finish it in a few minutes. The difficulty is clean execution, not insight.
Should I use binary search on the answer like the reported candidate?+
No. The reported interviewer pointed out that a direct sliding window solves it. Binary search with fixed-size window checks works, but it's extra code and extra bug surface. Go straight to the two-pointer window and save the time.
What edge cases should I test?+
Test a single-element array, an array of all identical values, an array with exactly two distinct values, and one where the best window sits at the end. Also check a case where a third value forces the left pointer to jump past a whole run of one number.
How do I prepare in 48 hours?+
Write the at-most-K-distinct window from memory until it's automatic, then fix K at two. Practice the delete-on-zero step specifically. Run your code on the two given examples and a few edge cases by hand. That covers this pattern.