Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard
Problem: A single-threaded CPU log has entries 'id:start:t' and 'id:end:t' (an end at t covers time unit t). Functions can call each other. Compute each function's exclusive time.
Problem: A single-threaded CPU log has entries 'id:start:t' and 'id:end:t' (an end at t covers time unit t). Functions can call each other. Compute each function's exclusive time.
def excl(n, logs):
res, st, prev = [0] * n, [], 0
for log in logs:
fid, typ, t = log.split(':'); fid, t = int(fid), int(t)
if typ == 'start':
if st: res[st[-1]] += t - prev
st.append(fid); prev = t
else:
res[st.pop()] += t - prev + 1; prev = t + 1
return res
print(excl(3, ["0:start:0", "1:start:2", "2:start:3", "2:end:4", "1:end:6", "0:end:9"]))What is printed?- A. [5, 3, 2]
- B. [4, 3, 3]
- C. [3, 4, 2]
- D. [5, 2, 2]
Correct answer: A. [5, 3, 2]
Function 0 runs units 0–1 and 7–9 (5 units); function 1 runs 2 and 5–6 (3 units); function 2 runs 3–4 (2 units). The '+1' on end entries and prev = t + 1 handle the inclusive end timestamps; total 10 = 0..9.
More Coding Interview Problems questions
- Problem: Evaluate an expression with +, −, , / (integer division truncating toward zero) and no parentheses, using a stack. python def…
- Problem: Find every start index in s of a substring that is a concatenation of all the given words (each exactly once, any order; all…
- Problem: Find the shortest contiguous substring of S that contains T as a SUBSEQUENCE (leftmost on ties). Scan forward to complete T, then…
- Problem: Find the longest substring that occurs at least twice (occurrences may overlap): binary search on the length with a set of…
- Problem: Count how many words are subsequences of s, by bucketing words by the next character they are waiting for. python from…
- Problem: Roads are one-way (a → b). Reorient the fewest roads so every city can reach city 0 (the roads form a tree). python def…