TikTok OA Interview Question: Partition Array by Pivot

19 Views
No Comments

Given an array A and a number b.

Return a reordered array such that all numbers before b are smaller than or equal to b, and all numbers after b are greater than or equal to b.

Example:

A = ["1", "2", "5", "3", "4", "8"], b = 3

Good: ["1", "2", "3", "4", "5", "8"] or ["2", "1", "3", "8", "5", "4"] or …

Bad: ["1", "3", "2", "4", "5", "8"]

This is a classic array partition problem. Given a pivot value b, you need to reorder the array so that every element on the left is less than or equal to b and every element on the right is greater than or equal to b. The relative order does not need to be preserved, so the natural solution is a partition-style scan, often using two pointers or a Dutch National Flag approach depending on the exact requirement.

END
 0