Luna and the Colorful Socks
Reported by candidates from PhonePe's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this PhonePe OA, reported in July 2026, is treating each requirement interval as its own little problem. Luna's intervals overlap, and overlapping ones force a single shared color across the whole merged stretch. Example 1 shows it: [1,3] and [3,5] collapse into all five socks. The real job is merge intervals, then pick the cheapest final color per merged group. It's a sorting plus counting problem wearing a story. If you spot the merge in the first two minutes, the rest is bookkeeping.
The problem
Luna has n socks arranged in a row. Sock i initially has color colors[i], where colors are numbered from 1 through k. Over m days, Luna chooses an inclusive interval [left, right]. After all repainting is complete, every sock in that interval must have the same color. Requirements from all days must hold simultaneously. A sock may be repainted at most once. Repainting any sock to color u costs repaintCost[u - 1]. A sock that already has the chosen final color does not need repainting and costs 0. Return the minimum total cost needed to satisfy every interval requirement. The practice interface uses these parameters: colors: the initial 1-indexed color value of each sock, stored in array order. requirements: each row contains one 1-indexed inclusive interval [left, right]. repaintCost: repaintCost[u - 1] is the cost to repaint one sock to color u. Return the minimum total cost as a long. Function minimumRepaintCost(colors: int[], requirements: int[][], repaintCost: long[]) → long Examples Example 1 colors = [1,2,1,3,2] requirements = [[1,3],[3,5]] repaintCost = [5,2,4] return = 6 The two intervals overlap at sock 3, so all five socks must end with one color. Choosing color 2 keeps two socks unchanged and repaints the other three for 3 * 2 = 6. The other final colors cost more. Example 2 colors = [1,2,1,3,2] requirements = [[1,2],[4,5]] repaintCost = [5,2,4] return = 4 The intervals are independent. Repainting sock 1 to color 2 costs 2, and repainting sock 4 to color 2 costs another 2. Constraints 1 <= colors.length <= 2 * 10^5 1 <= requirements.length <= 2 * 10^5 1 <= repaintCost.length <= 50 1 <= colors[i] <= repaintCost.length 1 <= requirements[i][0] <= requirements[i][1] <= colors.length 1 <= repaintCost[u] <= 10^9
Reported by candidates. Source: FastPrep
Pattern and pitfall
Here's the trick. Union overlapping intervals into disjoint groups. Use a difference array or sort by left endpoint. Intervals that only touch at one sock still merge, as Example 1 proves. Then for each group, count how many socks have each color. With k at most 50, try every final color u. Cost is (groupSize - count[u]) * repaintCost[u-1]. Take the minimum, sum across groups. Use prefix counts per color, or just scan each group once, since groups are disjoint and total length is at most n. Pitfalls: int overflow (costs reach 1e9 times 2e5, so use long), forgetting that touching intervals merge, and 1-indexing slips. Single-sock groups cost 0, so skip them. If you blank on the merge logic during the live OA, StealthCoder sits invisibly on your screen as a safety net and hands you the approach in real time.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Luna and the Colorful Socks 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass PhonePe's OA.
PhonePe 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.
Luna and the Colorful Socks FAQ
What's the core trick in Luna and the Colorful Socks?+
Merge overlapping intervals first. Any two intervals sharing even one sock must end with the same color, so they form one group. Then each group is independent: pick the final color that minimizes (group size minus existing count of that color) times its repaint cost.
How hard is this really?+
Medium. The code is short once you see the merge. The difficulty is realizing overlap chains into one big group. Constraints of 2e5 socks and only 50 colors point to an O(n log n + n*k) or better solution, which is comfortable.
Why can't I just solve each interval separately?+
Because requirements hold simultaneously. Intervals [1,3] and [3,5] share sock 3, so sock 3 has one final color, which forces all five socks to match. Solving separately gives a cheaper but invalid answer.
What overflow traps should I watch for?+
The answer type is long for a reason. Repaint cost goes up to 1e9 and you can repaint up to 2e5 socks, so the total reaches about 2e14. Multiply group counts as long before multiplying by cost, and keep the running sum in long.
How do I prepare for this in 48 hours?+
Practice interval merging until it's automatic, then a per-group frequency count over a small alphabet. Write the sort-and-sweep version once, test touching intervals and single-sock groups, and check your 1-indexed conversions. That covers nearly everything this problem tests.