
You are given two separate coding tasks from an interview.
You are given integer vectors A and B of the same length n (potentially very large). The vectors often contain long runs of the same value (i.e., many consecutive duplicates).
[-2, -2, -2, 5, 5, 0, 0, 0, 0]
as
[(-2,3), (5,2), (0,4)]
.
A
and
B
(without fully expanding them), compute the dot product:
A
and
B
(or their compressed forms) with the same length.
1 <= n <= 10^7
n
when there are long runs.
A = [1,1,1,2,2]
→
[(1,3),(2,2)]
B = [3,3,4,4,4]
→
[(3,2),(4,3)]
1*3 + 1*3 + 1*4 + 2*4 + 2*4 = 22
You are given n functions labeled 0..n-1 and a list of execution logs. Each log entry is a string:
"<id>:start:<timestamp>"
or
"<id>:end:<timestamp>"
Rules:
end
(i.e., an
end
at time
t
includes the unit time
t
).
n
, array
logs
of strings.
ans
of length
n
where
ans[i]
is the exclusive time of function
i
.
n = 2
logs = ["0:start:0","1:start:2","1:end:5","0:end:6"]
[3,4]
(Explanation: function 0 runs at times 0–1 and 6 → 3 units; function 1 runs at times 2–5 → 4 units.)