Reported February 2024
TikTokdivide and conquer

Find min Inversions

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

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

TikTok's February OA pulled a merge-sort classic: find the minimum number of inversions in an array. You're not asked to fix them, just count them. An inversion is any pair where a larger element appears before a smaller one. The naive O(n^2) brute force will time out on large inputs, which is why this problem exists. StealthCoder is your safety net if you blank on the merge-sort trick during the live assessment.

Pattern and pitfall

The pattern here is divide-and-conquer with merge-sort. While you iterate and count inversions naively in O(n^2) time, the optimal solution piggybacks on the merge step of merge-sort to count inversions in O(n log n) time. During the merge phase, whenever an element from the right subarray is picked before elements from the left subarray, those leftover elements form inversions with it. Track the count as you merge. Common pitfall: overthinking it as a sorting problem instead of recognizing it as a counting problem baked into a merge operation. StealthCoder reads your problem and delivers the merge-sort inversion pattern instantly if you're stuck mid-OA.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Find min Inversions 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 StealthCoder

Related leaked OAs

⏵ The honest play

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 min Inversions FAQ

Is the brute force O(n^2) solution fast enough for TikTok's OA?+

Depends on the constraints, but typically no. If n is above a few thousand, you'll timeout. The optimal merge-sort O(n log n) approach is the expected solution. Have both in your head, code the fast one first.

What exactly is an inversion here?+

Any pair of indices (i, j) where i is less than j but array[i] is greater than array[j]. [3, 1, 2] has two inversions: (3,1) and (3,2). Count all such pairs.

Do I need to track the actual pairs or just the count?+

Just the count. TikTok's problem asks for the minimum number of inversions, not which elements form them. This simplifies the solution significantly.

How do I count inversions during merge-sort?+

When merging two sorted halves, if an element from the right half is smaller than an element in the left half, all remaining elements in the left half form inversions with it. Add that count to your inversion total.

Can I solve this with a different approach, like a modified merge-sort or segment tree?+

Yes. Merge-sort is the most common and intuitive. Segment trees or Fenwick trees with coordinate compression work too but are overkill. Stick with merge-sort unless you're very comfortable with tree-based solutions.

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

OA at TikTok?
Invisible during screen share
Get it