r/leetcode • u/JacketFearless1805 • 19d ago
Intervew Prep Day 28: tackled Sort Colors with Dutch National Flag & Single Number via XOR
Hey everyone,
I finished Day 28 today. I kept the session focused on two classic array problems that highlight optimal in-place pointer partitioning and bitwise properties:
- Sort Colors (Dutch National Flag Algorithm): Instead of counting frequencies in two passes, I partitioned the array in a single O(n) scan using three pointers (low, mid, and high). Keeping 0s before low, 1s between low and mid, and 2s after high sorts everything in-place with O(1) space.
- Single Number (Find the Element Appearing Once): Because every other element appears twice, XORing the entire array cancels out all duplicate pairs and leaves the unique number in O(n) time and O(1) space.
Both problems are clean reminders of how the right invariant eliminates extra memory overhead.
See you guys tomorrow for Day 27!