Pair Left and Right Socks by ID
Updated
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, use each sock at most once and make as many pairs as possible. Use these rules to choose the pairs: 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, andsides.length = colors.length = ids.length.- Every side is exactly
LorR. - IDs are distinct integers with
0 <= ids[i] <= 10^9. - Each color contains
1to16English letters, digits or underscores.
More Roblox Onsite Interview questions
- Most Frequent Call Path From Function Trace LogsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Search Autocomplete SystemPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Candy Crush Grid Matching and GravityPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Topological Sort with Secondary OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Most Frequent Call Stack Per ThreadPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Count and Score Distinct Hat AssignmentsONSITE INTERVIEW · Seen Sep 2026
- Rate Limiter Sliding Window With Per-Entity LimitsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Most Frequent Call Path in Interleaved LogsONSITE INTERVIEW · Seen Aug 2026
- Grid Pathfinding with Obstacles (DFS)PHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2026
- Maximize Distance to Closest Person — Return the SeatONSITE INTERVIEW · Seen Jun 2026
Similar questions at other companies
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Filter Anagrams of a TargetMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Rank Transform an ArrayAgoda · ONSITE INTERVIEWAgoda interview questions
- Deduplicate Logs: Keep FirstGoogle · ONSITE INTERVIEWGoogle interview questions
- Loyal Customers by Two-Day Product VarietyAmazon · ONSITE INTERVIEW
- Restaurant Vote LeadersZipRecruiter · PHONE SCREEN · ONSITE INTERVIEWZipRecruiter interview questions
- High-Severity Counts by Search TypeChainalysis · ONSITE INTERVIEWChainalysis interview questions
- Intersection of Two ListsJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- Net Position Calculation from API SnapshotsHeadlands Technologies · ONSITE INTERVIEWHeadlands Technologies interview questions
- Count Word FrequenciesBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions