Find Pair with Max GCD
Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
TikTok asked this in August and it's a math problem wrapped as an array search. You're finding the pair of numbers with the maximum greatest common divisor (GCD). The instinct is to brute force every pair and compute GCD for each, which works for small inputs but falls apart at scale. The trick is understanding that the answer is almost always one of the larger numbers in the array, and you can optimize by iterating from the max value down and checking divisibility rather than comparing every single pair.
Pattern and pitfall
The brute force approach (check all pairs, compute GCD) is O(n^2 * log m) where m is the max element. It's slow. The optimization: iterate from the largest possible GCD down to 1. For each candidate GCD value, check if at least two numbers in the array are divisible by it. Use a hash set for O(1) lookup. The moment you find a value where two array elements are divisible, that's your answer. This flips the problem from pair-centric to GCD-centric. StealthCoder will flag this pattern instantly during your OA, so you won't waste time coding a brute force solution that times out.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Find Pair with Max GCD 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 TikTok's OA.
TikTok 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.
Find Pair with Max GCD FAQ
How hard is this problem really?+
Medium. Brute force is easy to code and passes small test cases. The challenge is recognizing that you need to optimize to GCD-down iteration, not pair iteration. TikTok likely has test cases with large arrays or large numbers designed to break the naive approach.
What's the core trick?+
Stop thinking about pairs. Start thinking about candidate GCD values. Iterate from max down to 1, and for each value, check if two or more array elements are divisible by it using a hash set. The first match is your answer.
Do I need to know advanced math?+
No. You need to know GCD (Euclidean algorithm, built into most languages as math.gcd or similar) and divisibility. That's it. The insight is algorithmic, not mathematical.
What's a common mistake?+
Computing GCD for every pair without optimization. Also forgetting to check that you have at least two distinct elements divisible by the candidate GCD. Edge case: what if the array has duplicates?
How do I prep in 48 hours?+
Implement brute force first and time it. Then code the GCD-down approach. Test both on a small array (e.g., [2, 4, 6, 8]). Understand why the optimized version is faster. You don't need to memorize; you need to see the pattern.