Deduplicate Card-Pile Prefixes
Updated
Problem statement
Process card piles from left to right. For pile i, its cleaned first k cards must contain no value that appears among the cleaned first k cards of any of the previous d piles.
Clean a pile by scanning it from left to right and deleting the minimum possible number of conflicting cards until either k valid prefix cards have been kept or the pile ends. Once k valid prefix cards have been kept, keep the rest of the pile unchanged, because cards after the first k kept cards do not take part in comparisons.
The first pile has no earlier restrictions. Return all cleaned piles in their original order. When fewer than d earlier piles exist, compare against all of them. k may exceed a pile's final length.
Function
cleanPilePrefixes(piles: int[][], k: int, d: int) → int[][]Examples
Example 1
piles = [[3,2,4,5,2],[2,1,3,4]]k = 2d = 1return = [[3,2,4,5,2],[1,4]]The second pile deletes 2 and 3 so its cleaned first two cards, [1,4], avoid [3,2].
Example 2
piles = [[1,2,9],[2,3,8],[1,4,7,5]]k = 2d = 2return = [[1,2,9],[3,8],[4,7,5]]Each later prefix avoids the union of up to two previous cleaned prefixes.
Example 3
piles = [[1],[1,1],[1,2]]k = 2d = 1return = [[1],[],[1,2]]The empty cleaned second pile contributes no forbidden prefix values to the third pile.
Constraints
1 <= piles.length <= 100000.- The total number of cards is at most
200000. 0 <= k,d <= 100000.- Card values are signed 32-bit integers.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Sliding Window MedianNotion · ONSITE INTERVIEWNotion interview questions
- Sliding Window MedianBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
- Count Zero Request ServersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Longest Subarray with K Distinct ValuesAmazon · ONSITE INTERVIEW
- Count Equal Code Pairs Within Distance KInfosys · ONSITE INTERVIEWInfosys interview questions
- Longest Subarray with At Most Two Distinct ValuesMoody's Investors Service · ONSITE INTERVIEW
- Maximum Sum of a Unique-Element SubarrayTekion · ONSITE INTERVIEWTekion interview questions
- Sliding Window: Target Containment and Most-Repeated WindowRoblox · ONSITE INTERVIEWRoblox interview questions
- Duplicate Transactions Within 60 SecondsCapital One · ONSITE INTERVIEWCapital One interview questions