How do you count inversions in an array in O(n log n)?
PICTURE THIS: GIT FLOW
Simple meaning
During merge sort, when you take a value from the right half, every remaining left-half value is a larger-before-smaller inversion.
WHY — Sorting instead of guessing?
Why interviewers care about Sorting:
question about Sorting.
trade-offs, and what you would actually do on a DSA project - not buzzwords.
Name the idea, why it exists, then one short example.
End with when you use it and one common pitfall.
STEPS — What happens step by step?
Before you speak the answer, walk the interviewer through these steps:
- 1During merge sort, when
you take a value from the right half, every remaining left-half value is a larger-before-smaller inversion.
- 2Why it exists
Add that count while merging.
- 3Time is O(n log
n) and space is O(n) for merge buffers.
- 4Give an example
Nested loops are O(n^2).
- 5Common mistake
What juniors usually get wrong.
- 6Close
When you pick this over the alternative.
EXAMPLE — See it in action
Here's a short line you can speak, broken into clear beats:
Note: Adapt this scaffold to your own project — keep it under 60–90 seconds.
Key takeaway
During merge sort, when you take a value from the right half, every remaining left-half value is a larger-before-smaller inversion. Add that count while merging.