Reported July 2025
Googlecounting

Minimum Swaps to Sort a Ternary Array After Updates

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

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

Google's July 2025 OA reports include a ternary array where every value is 1, 2, or 3, and the queries keep rewriting positions. After each point update you need the minimum swaps to sort the whole array, with up to 10^5 elements and 10^5 queries. Re-sorting per query dies on the constraints. The real answer is a counting problem dressed up as sorting, and it can be maintained in O(1) per update. If you blank on the bookkeeping live, StealthCoder is the invisible safety net on the OA screen. Here's the pattern so you don't need it.

The problem

You are given an integer array nums containing only 1, 2, and 3, together with a sequence of point-update queries.
Each query is [position, value]:
position is a one-based index into nums.
value is the new value assigned at that position.
Apply the queries cumulatively in their given order. After each update, find the minimum number of swaps required to sort the entire current array in nondecreasing order. One swap may exchange the values at any two positions.
Return an array containing one minimum-swap count for each query.

Function
minimumSwapsAfterUpdates(nums: int[], queries: int[][]) → int[]

Examples
Example 1
nums = [1,3,2]
queries = [[2,2],[1,3]]
return = [0,1]
After the first update, the array is [1, 2, 2], which is already sorted. After the second update, it is [3, 2, 2]; swapping the first and third positions produces [2, 2, 3].
Example 2
nums = [3,2,1,3,2,1]
queries = [[3,3],[6,2],[1,1]]
return = [2,2,2]
Each update changes the segment boundaries of the sorted target array. In all three resulting arrays, two arbitrary-position swaps are necessary and sufficient.
Example 3
nums = [2,1,3,1]
queries = [[4,3],[3,2],[1,1]]
return = [1,1,0]
The first two updated arrays each need one swap. The final update produces [1, 1, 2, 3], so its answer is 0.

Constraints
1 <= nums.length <= 10^5.
1 <= queries.length <= 10^5.
Every value in nums is 1, 2, or 3.
Every query contains exactly two integers [position, value].
1 <= position <= nums.length.
value is 1, 2, or 3.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: the sorted target is fixed by counts. Let c1, c2, c3 be the counts of each value. Target zones are the first c1 slots, next c2, last c3. Keep a 3x3 matrix m[a][b] = number of positions in zone a currently holding value b. Off-diagonal entries are misplaced elements. Swap directly any pair (a,b) with (b,a): each costs one swap and fixes two elements. What remains forms 3-cycles, each costing two swaps. So answer = sum of min(m[a][b], m[b][a]) over pairs, plus 2 times the leftover misplaced count divided by 3. A point update changes the counts, which moves the zone boundaries. Rebuilding the matrix from scratch per query is O(n), which is too slow. Recompute the matrix using prefix counts of each value over the fixed index ranges. Pitfall: forgetting that boundaries shift after every update, or using the one-based position as zero-based. The matrix only has 9 cells, so a Fenwick tree per value gives O(log n) range counts.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Minimum Swaps to Sort a Ternary Array After Updates 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Google reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Swaps to Sort a Ternary Array After Updates FAQ

What's the trick for Minimum Swaps to Sort a Ternary Array After Updates?+

Sorted order depends only on counts of 1s, 2s, and 3s. Build a 3x3 matrix of how many elements sit in the wrong zone. Pair mutual mismatches for one swap each, and the leftover forms 3-cycles at two swaps each. Never actually sort.

How hard is this Google OA question really?+

Medium-hard. The cycle-counting idea is the main hurdle, and the update handling adds a second layer. Once you see the 3x3 matrix, the code is short. Most failures come from stale zone boundaries after an update.

How do I handle the updates efficiently?+

Keep the counts of each value and a Fenwick tree per value, or the 3x3 matrix with prefix counts. After each update, adjust counts, recompute the three zone boundaries, and query how many of each value fall in each zone. That's O(log n) per query instead of O(n).

Why is the leftover multiplied by two thirds?+

After pairing direct swaps, every remaining misplaced element belongs to a 3-cycle like 1 to 2 to 3 to 1. A 3-cycle needs two swaps to fix and covers three misplaced elements. So the leftover count divided by 3, times 2, gives the extra swaps.

How do I prepare for this in 48 hours?+

Do the minimum swaps to sort a binary array, then extend it to three values with the matrix idea. Write the brute force first to verify against the examples, like [3,2,1,3,2,1] giving 2. Then swap in the prefix-count version and test one-based indexing.

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

OA at Google?
Invisible during screen share
Get it