Data Structures & AlgorithmsHardAmazon · Microsoft OA

Problem: Can the cards be split into groups of W consecutive values?

Problem: Can the cards be split into groups of W consecutive values?
from collections import Counter
def straights(hand, W):
    if len(hand) % W: return False
    c = Counter(hand)
    for x in sorted(c):
        k = c[x]
        if k:
            for v in range(x, x + W):
                if c[v] < k: return False
                c[v] -= k
    return True
print(straights([1, 2, 3, 3, 4, 4, 5, 6], 4), straights([1, 2, 3, 4, 5, 6, 8, 9, 10], 3))
What is printed?
  1. A. False True
  2. B. False False
  3. C. True True
  4. D. True False

Correct answer: C. True True

The smallest remaining card must start a group. First hand: 1 starts 1-2-3-4, leaving 3, 4, 5, 6 → another run: True. Second: 1-2-3, 4-5-6 and 8-9-10, so also True; the gap at 7 does not matter because no group needs to cross it.

More Coding Interview Problems questions

Exams that ask this topic