FastPrepLuna and the Colorful Socks
Problems/Luna and the Colorful Socks

Luna and the Colorful Socks

PhonePe logoPhonePe● HardOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a PhonePe online assessment. The report does not give a function signature or an example. Our version is about 97% the same as the reported question.

Luna has n socks in a row, where n is the length of colors. Socks are counted from the left, and colors lists their starting colors in that order. So the first sock has the first color in colors, the second sock has the second, and so on. Colors are numbered from 1 through k, where k is the length of repaintCost.

There are m days, one for each row of requirements. On each day, Luna picks an interval [left, right] of socks. When all repainting is done, every sock in that interval must have the same color. The intervals from all days must hold at the same time.

Each sock can be repainted at most once. Repainting a sock to color u costs repaintCost[u - 1]. A sock that already has its final color does not need repainting, so it costs 0.

Return the smallest total cost that meets every interval requirement.

Your function gets these inputs:

  • colors: the starting color of each sock, in row order. Color values start at 1.
  • requirements: each row is one interval [left, right]. Sock positions here start at 1, and both ends are included.
  • 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 share sock 3, so all five socks must end up with one color. Picking color 2 leaves two socks as they are and repaints the other three, for a total of 3 * 2 = 6. Every other final color costs more, so this total is the answer.

Example 2

colors = [1,2,1,3,2]requirements = [[1,2],[4,5]]repaintCost = [5,2,4]return = 4

The intervals do not overlap, so each one is handled on its own. In the first interval, repainting sock 1 to color 2 costs 2. In the second, repainting sock 4 to color 2 costs another 2. The sum of these two repaints is the answer.

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

More PhonePe interview questions

  • Constrained Jump SequencesOA · Seen Aug 2026
  • Maximum Payout with Context SwitchesOA · Seen Aug 2026
  • Substrings Without a Target SubsequenceOA · Seen Aug 2026
  • Maximum Sum with K-Element SkippingONSITE INTERVIEW · Seen Dec 2026
  • Minimum Number of Days to Make m BouquetsONSITE INTERVIEW · Seen Dec 2026
  • Evaluate DivisionONSITE INTERVIEW · Seen Oct 2026
  • K-th Smallest Value in a Sorted MatrixONSITE INTERVIEW · Seen Oct 2026
  • Identify Two Failing Tests From an Oracle TranscriptONSITE INTERVIEW · Seen Sep 2026
  • Count Bowl SubarraysONSITE INTERVIEW · Seen Jul 2026
  • Reconstruct ItineraryONSITE INTERVIEW · Seen Jul 2026

Similar questions at other companies

  • Hard Disk Clustering CostConfluent · OAConfluent interview questions
  • Maximize K-Element AND with an Increment BudgetAmazon · OA
  • Delete and Freeze GameRubrik · OARubrik interview questions
  • Most Segments That Sort the ArrayRippling · OARippling interview questions
  • Most Items Within Budget from a Start IndexHudson River Trading · OAHudson River Trading interview questions
  • Minimum Starting Coins to Buy All ProductsMicrosoft · OAMicrosoft interview questions
  • Minimum Swaps to Sort a Ternary Array After UpdatesGoogle · OAGoogle interview questions
  • Disk ClusteringAirbnb · OAAirbnb interview questions
  • Get Max EfficiencySalesforce · OASalesforce interview questions
  • Finding a Hidden Benchmark ValueSamsung · OASamsung interview questions
All PhonePe interview questionsSee PhonePe hiring insights
Saved
class Solution {
    public long minimumRepaintCost(int[] colors, int[][] requirements, long[] repaintCost) {
      // write your code here
    }
}
Checking account…