Problem · Array

Deduplicate Homepage Content Across Rows

Learn this problem
MediumNetflix logoNetflixFULLTIMEPHONE SCREEN

Problem statement

You are given ordered homepage content rows in rows. Process the rows from top to bottom and the titles within each row from left to right.

For an ordinary row, its first globalLimits[r] retained titles participate in global deduplication:

  • If a title has already been retained in the global portion of an earlier ordinary row, skip it.
  • Otherwise, retain it and add it to the global set.

After the row has retained its global-limit titles, retain later titles only if that title has not already been retained in the same row. A title rejected by global deduplication does not count toward the row's retained-title limit.

If exemptRows[r] is true, the entire row uses only row-local deduplication and neither reads nor changes the global set.

For this exercise, assume titles are compared by exact, case-sensitive string equality. In every row, retain the first eligible occurrence of each title. Return all filtered rows in their original order.

Function

deduplicateHomepageRows(rows: String[][], globalLimits: int[], exemptRows: boolean[]) → String[][]

Examples

Example 1

rows = [["A","B","A","C"],["B","D","B","E"],["A","F","A"]]globalLimits = [2,2,1]exemptRows = [false,false,true]return = [["A","B","C"],["D","E"],["A","F"]]

The first row globally retains A and B, then retains C locally. In the second row, global duplicates of B are skipped, so D and E fill its two global positions. The third row is exempt and keeps the first row-local occurrence of A and F.

Example 2

rows = [["x","x","y"],["x","z","x"]]globalLimits = [0,0]exemptRows = [false,false]return = [["x","y"],["x","z"]]

Both global limits are zero, so each row independently keeps only its first occurrence of each title.

Constraints

  • rows.length == globalLimits.length
  • rows.length == exemptRows.length
  • Every value in globalLimits is nonnegative.
  • Titles are compared using exact, case-sensitive string equality.

More Netflix problems

drafts saved locally
public String[][] deduplicateHomepageRows(String[][] rows, int[] globalLimits, boolean[] exemptRows) {
    // Your code here
}
rows[["A","B","A","C"],["B","D","B","E"],["A","F","A"]]
globalLimits[2,2,1]
exemptRows[false,false,true]
expected[["A", "B", "C", "D", "E", "A", "F"]]
checking account