Reported August 2026
Matroidsliding window

Count Good Tuples

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

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

The Matroid OA reported in August 2026 looks like it wants a clever algorithm, then hands you a sliding window of size three. Count the consecutive triples where exactly two values match. The statement says O(n^2) is fine, which tells you brute force isn't the trap here. The trap is the wording. Exactly two equal means all-equal triples don't count, and that's where people lose points. If you read the problem carefully, it's ten lines of code. If you blank on the edge case, StealthCoder is the safety net running invisibly during the live assessment.

The problem

You are given an integer array a. Consider every tuple of three consecutive elements.
A tuple is good if exactly two of its three values are equal. For example, (2, 1, 2) is a good tuple, while (1, 1, 1) and (1, 2, 3) are not.
Return the number of good tuples in a. Tuples may overlap.
A solution with time complexity no worse than O(a.length^2) fits within the execution time limit.

Function
solution(a: int[]) → int

Examples
Example 1
a = [1, 1, 1, 2, 1, 3, 4]
return = 2
The five consecutive tuples are:
(1, 1, 1): all three values are equal, so this is not a good tuple.
(1, 1, 2): exactly two values are equal, so this is a good tuple.
(1, 2, 1): exactly two values are equal, so this is a good tuple.
(2, 1, 3): all three values are distinct, so this is not a good tuple.
(1, 3, 4): all three values are distinct, so this is not a good tuple.
Exactly two tuples are good, so the answer is 2.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Slide a window of three across the array. For each index i from 0 to n-3, take x=a[i], y=a[i+1], z=a[i+2]. The tuple is good when exactly one pair matches. The cleanest check is (x==y) + (y==z) + (x==z) == 1. If all three are equal, that sum is 3. If all are distinct, it's 0. Two equal values give exactly 1. That kills the most common bug, which is counting (1,1,1) as good because two of its values match. Arrays shorter than 3 return 0, so don't index past the end. Overlap is allowed, so you don't skip ahead after a hit. The solution is O(n) time and O(1) space, well inside the stated bound. If the live OA rattles you and you second-guess the condition, StealthCoder can hand you the pattern from the screen.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Count Good Tuples 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 StealthCoder

Related leaked OAs

⏵ The honest play

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

Matroid 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.

Count Good Tuples FAQ

How hard is the Count Good Tuples question really?+

Easy. It's a fixed-size window of three with one comparison. The difficulty is reading 'exactly two equal' correctly and not counting triples where all three match. Most candidates finish it fast once they write the condition cleanly.

What's the trick to getting it right?+

Count equal pairs among the three values. Add (x==y), (y==z), and (x==z). If the total is exactly 1, the tuple is good. All-equal gives 3 and all-distinct gives 0, so both get rejected automatically.

Do I need anything better than O(n)?+

No. The problem allows up to O(n^2), but a single pass over the array is O(n) and uses constant extra space. Nested loops or extra data structures are unnecessary, so keep it simple and avoid bugs.

What edge cases should I test?+

Test an array shorter than three elements, which should return 0. Test all-equal arrays like [1,1,1,1], which should return 0. Test overlapping good tuples, since they're counted separately. Run the provided example and confirm you get 2.

How do I prepare for this Matroid OA in 48 hours?+

Practice fixed-size sliding window problems and boundary handling on loops. Write the pair-count condition from memory and trace the example by hand. Spend the rest of your time on harder questions, since this one is quick if you read it carefully.

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

OA at Matroid?
Invisible during screen share
Get it