Unique Rows of a Binary Matrix
Updated
Problem statement
You are given a matrix matrix that holds only 0 and 1. Two rows are the same when they have the same value in every column.
Return the different rows of the matrix, each one exactly once. Keep the rows in the order in which they first appear in matrix: when a row repeats, keep its first copy and drop the later ones.
Return the kept rows as a matrix. The rows are not sorted.
Function
uniqueRows(matrix: int[][]) → int[][]Examples
Example 1
matrix = [[0,1,1,0],[1,0,0,1],[0,1,1,0],[1,1,1,1],[1,0,0,1]]return = [[0,1,1,0],[1,0,0,1],[1,1,1,1]]Row 0 is [0, 1, 1, 0] and is new. Row 1 is [1, 0, 0, 1] and is new. Row 2 repeats row 0, so it is skipped. Row 3 is [1, 1, 1, 1] and is new. Row 4 repeats row 1, so it is skipped. The kept rows, in the order they first appear, are [[0, 1, 1, 0], [1, 0, 0, 1], [1, 1, 1, 1]].
Example 2
matrix = [[1,1],[0,1],[1,0],[0,0]]return = [[1,1],[0,1],[1,0],[0,0]]All four rows are different, so every row is kept. The rows stay in the order they were given; they are not sorted.
Example 3
matrix = [[1],[1],[0],[1],[0]]return = [[1],[0]]Every row has one column. The first row [1] is kept, the second repeats it, the third [0] is new and kept, and the last two repeat rows that were already seen. The answer is [[1], [0]].
Constraints
1 <= matrix.length <= 3001 <= matrix[i].length <= 40matrix[i][j]is0or1.- All rows have the same length.
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024
Similar questions at other companies
- Sudoku Board ValidationConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
- Validate Rectangular Game PiecesGoogle · ONSITE INTERVIEWGoogle interview questions
- Set Matrix ZeroesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Cache Shared Image Pipeline PrefixesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Query Whether an Island Size ExistsMeta · ONSITE INTERVIEWMeta interview questions
- Validate a Sudoku BoardZipRecruiter · ONSITE INTERVIEWZipRecruiter interview questions
- Unique Pairs in a 2D Matrix Summing to TargetAmazon · PHONE SCREEN
- Sparse Matrix MultiplicationByteDance · PHONE SCREENByteDance interview questions
- First Completed Mingo LineEpic · OAEpic interview questions
- Valid SudokuVerkada · PHONE SCREENVerkada interview questions
Expedia interview experiences
- Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
- Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
- Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
- Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
- Expedia Intern Final Round in 2023: 17 Experiences (Part 2 of 6)
- Expedia SWE Intern Final Round: 126 Experiences (Part 1 of 6)