Data Structures & AlgorithmsHardAmazon · Microsoft OA
Problem: A CPU runs tasks (letters); identical tasks need at least n idle-or-other slots between them. Find the minimum total time slots.
Problem: A CPU runs tasks (letters); identical tasks need at least n idle-or-other slots between them. Find the minimum total time slots.
from collections import Counter
def least(tasks, n):
f = Counter(tasks).values(); mx = max(f)
cnt_mx = sum(1 for v in f if v == mx)
return max(len(tasks), (mx - 1) * (n + 1) + cnt_mx)
print(least("AAAABBBCCD", 3))What is printed?- A. 10
- B. 13
- C. 16
- D. 12
Correct answer: B. 13
A appears 4 times, so it needs 3 full frames of length n + 1 = 4 plus a final A: 3 × 4 + 1 = 13. The other 6 tasks fit into the gaps, leaving 3 idle slots. The max(len, …) term matters only when tasks are plentiful enough to fill every gap.
More Coding Interview Problems questions
- Problem: Children stand in a line with ratings; each gets ≥ 1 candy and a child with a higher rating than a NEIGHBOUR must get more than…
- Problem: Count the ways to make amount 5 from coins [1, 2, 5]. A developer wrote the loops in this order. python def ways(coins, amt): dp…
- Problem: Find the length of the longest palindromic subsequence of s and the minimum number of insertions that make s a palindrome. python…
- Problem: Matrices A1..A6 have dimensions given by d (Ai is d[i−1] × d[i]). Find the minimum number of scalar multiplications to compute…
- Problem: With e identical eggs, find the minimum number of drops that always identifies the critical floor of an f-floor building. The…
- Problem: Count the ways to place n non-attacking queens on an n × n board. python def queens(n): cols, d1, d2 = set(), set(), set() def…