Count Twin Pairs
Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
TikTok's January 2025 OA asked candidates to count twin pairs, a classic counting problem that sounds simpler than it is. You're probably expecting a brute force scan, but there's a pattern hiding in the constraint. Twin pairs usually means numbers that differ by a fixed amount (often 1, sometimes a specific value). The trick is recognizing whether you need a hash table for fast lookups or a sorting pass. If you blank on the approach during the live OA, StealthCoder will read the exact problem statement and surface the pattern in seconds, so you don't lose time to panic.
Pattern and pitfall
Twin pairs boil down to counting unique pairs of numbers that satisfy a distance condition. The naive approach is O(n squared), checking every combination. The real solution uses a hash table or sorted array to look up whether a complement exists for each number in one pass. If the problem specifies twin pairs as numbers differing by exactly 1, sort and iterate, counting consecutive equal or near-equal elements. If it's a custom distance, load numbers into a set or map and check membership for each target offset. The pitfall: forgetting to handle duplicates correctly, or miscounting pairs when the same number appears multiple times. StealthCoder acts as your safety net if you freeze on the exact definition of 'twin' or the edge case handling during the live assessment.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Count Twin Pairs 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
This OA pattern shows up on LeetCode as contains duplicate ii. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass TikTok's OA.
TikTok 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.
Count Twin Pairs FAQ
What's the actual definition of 'twin pairs' in this problem?+
Twin pairs usually means two numbers that differ by exactly 1 (or a specified constant). Without the full problem text, assume distance of 1. If it's custom, the problem statement will clarify. The exact definition determines whether you sort-and-scan or use a hash set lookup.
Is this a one-pass hash table problem or a sorting problem?+
Both work. Hash set is O(n) time and space; sorting is O(n log n) time, O(1) space. For TikTok, hash table is typically faster to code and less error-prone. Load all numbers into a set, then iterate and check if target exists.
What's the duplicate trap?+
If the input has duplicates, don't double-count the same pair. If twin pairs means distance of 1 and you see [1, 1, 2], that's one pair (1, 2), not two. Use a set to deduplicate first or track which pairs you've already counted.
Can I solve this in 10 minutes under pressure?+
Yes. Hash table approach is textbook: iterate once to build a set, iterate again to count matches. Write it once, test on a small example (like [1, 2, 3, 5, 6]), submit. No tricky greedy or DP needed.
Does TikTok care about the coding style or just correctness?+
Correctness and efficiency. They check for the right time complexity and correct edge case handling. Clean variable names help. They don't dock for style, but slow algorithms or off-by-one errors will fail test cases.