Data Structures & AlgorithmsModerateProduct Company SDE · Amazon/Microsoft-style OA
Problem: Tasks labelled by letters each take 1 unit; the same task needs n idle units between repeats. Find the least total time.
Problem: Tasks labelled by letters each take 1 unit; the same task needs n idle units between repeats. Find the least total time.
from collections import Counter
def least(tasks, n):
c = Counter(tasks).values()
mx = max(c); cnt_mx = sum(1 for v in c if v == mx)
return max(len(tasks), (mx - 1) * (n + 1) + cnt_mx)
print(least("AAABBB", 2), least("AAABBB", 0), least("AAAABBBCCDE", 2))What is printed?- A. 8 6 10
- B. 6 6 11
- C. 8 8 11
- D. 8 6 11
Correct answer: D. 8 6 11
The most frequent task forms mx − 1 full frames of size n + 1, plus one slot per task tied for the maximum. AAABBB, n = 2: 2 × 3 + 2 = 8 (AB_AB_AB). With n = 0 no idling is needed: 6. For AAAABBBCCDE: 3 × 3 + 1 = 10 < 11 tasks, so the other tasks fill all gaps and the answer is 11. O(n).
More Coding Interview Problems questions
- Problem: Merge overlapping booking intervals. python def merge(iv): iv.sort(); out = [] for s, e in iv: if out and s <= out[-1][1]…
- Problem: Remove the fewest meetings so that the rest do not overlap (meetings that only touch at an endpoint are fine). python def…
- Problem: Find the minimum number of meeting rooms needed for a list of meetings [start, end). python import heapq def rooms(iv)…
- Problem: A car starts with an empty tank at one of n petrol pumps on a circular route; pump i gives gas[i] and the trip to the next costs…
- Problem: Each child must get at least one sweet, and a child with a higher rating than a neighbour must get more than that neighbour. Find…
- Problem: Each position holds the maximum jump length from it. Return the minimum jumps to reach the last index. python def jumps(a): j =…