FastPrepPair Left and Right Socks by ID

Pair Left and Right Socks by ID

Roblox logoRoblox● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

Each sock has a side, a color and a unique integer ID. The parallel arrays sides, colors and ids describe the socks. A valid pair contains one left sock (L) and one right sock (R) of exactly the same color.

For this exercise, assume each sock may be used at most once and form as many disjoint pairs as possible. To make the result deterministic, sort left IDs and right IDs separately within each color, then pair the smallest left with the smallest right, the next-smallest with the next-smallest, and so on until one side is exhausted.

Return the pairs as [leftID, rightID] rows, ordered by increasing left ID across all colors. Unmatched socks are omitted. Color identifiers are case-sensitive; return an empty array when no pair can be formed.

Function

pairSockIds(sides: String[], colors: String[], ids: int[]) → int[][]

Examples

Example 1

sides = ["L","R","L","R"]colors = ["red","blue","blue","red"]ids = [7,2,4,9]return = [[4,2],[7,9]]

Blue pairs left 4 with right 2; red pairs left 7 with right 9. Increasing left ID puts the blue pair first.

Example 2

sides = ["L","L","R","R","L"]colors = ["red","red","red","red","red"]ids = [8,3,6,1,5]return = [[3,1],[5,6]]

Left IDs are 3, 5 and 8; right IDs are 1 and 6. Two pairs are possible, leaving left 8 unmatched.

Example 3

sides = []colors = []ids = []return = []

With no socks, no pairs can be formed.

Constraints

  • 0 <= ids.length <= 500, and sides.length = colors.length = ids.length.
  • Every side is exactly L or R.
  • IDs are distinct integers with 0 <= ids[i] <= 10^9.
  • Each color contains 1 to 16 English letters, digits or underscores.

More Roblox problems

See Roblox hiring insights
public int[][] pairSockIds(String[] sides, String[] colors, int[] ids) {
    // write your code here
}
sides["L","R","L","R"]
colors["red","blue","blue","red"]
ids[7,2,4,9]
expected[[4,2],[7,9]]
Checking account…