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?- A. False True
- B. False False
- C. True True
- 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
- Problem: Report the median of every window of size k (sorted-list version with bisect). python from bisect import insort, bisectleft def…
- Problem: A single-threaded CPU receives tasks [enqueueTime, processingTime]. When idle it runs the available task with the shortest…
- Problem: A min-stack supports push, pop, top and getMin in O(1) by storing (value, current minimum) pairs. python st, out = [], [] def…
- Problem: Josephus problem: n people in a circle, every k-th person is eliminated; find the 1-based position of the survivor. python def…
- Problem: Count permutations of 1..n where, for every position i (1-based), the value is divisible by i or i is divisible by the value…
- Problem: Each candidate can be used at most ONCE and the list has duplicates; count the distinct combinations that sum to the target…