Handle cards, islands, Trie, median
Company: Snapchat
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: These problems evaluate proficiency in core data structures and algorithmic problem-solving, covering grouping/counting techniques, grid-based island detection, trie (prefix tree) design, and streaming median computation within the domain of data structures and algorithms.
Constraints
- 1 <= len(deck) <= 10000
- 0 <= deck[i] <= 10000
- Return a boolean indicating whether such a partition exists
- Time expectation: O(n) where n = len(deck)
Solution
from typing import List
from collections import Counter
from math import gcd
def can_partition_deck(deck: List[int]) -> bool:
if len(deck) < 2:
return False
counts = Counter(deck).values()
g = 0
for c in counts:
g = gcd(g, c)
if g == 1:
return False
return g >= 2
Explanation
Time complexity: O(n). Space complexity: O(k) where k is the number of distinct card values.
Hints
- Count the frequency of each distinct card value.
- A partition into equal-sized same-value groups is possible if and only if the greatest common divisor (gcd) of all frequencies is at least 2.
- Use a running gcd over frequency counts to short-circuit when it becomes 1.