Google Phone Screen Question | L4 | Bangalore | Passed | oct 2024
Company: Google
Role: L4
Interview Stage: Phone Screen
Location: Bangalore
Year: 2024
Original Post Content:
Yesterday, I had a Google phone screen interview. The interviewer joined 5 minutes late, but she was really sweet. After introducing herself and asking for my name, she jumped straight into the question.
Problem
You are given a timeline with events where certain users (including a driver) require a resource at specific intervals. There are N refilling points on the timeline, each occurring at a specified time, where the resource can be refilled. Initially, the resource supply is limited.
The timeline starts at time 0 and ends at a given time X.
Users require 1 unit of the resource at certain times. After each consumption, they will need 1 unit of the resource again after a fixed interval.
If a user’s resource demand is not met at the required time, a penalty is incurred for that user.
Your task is to determine how much resource should be added at the start and at the refilling points to minimize the total cost, which includes both the cost of adding the resource and the penalties for unmet demands.
- X: The end time.
- N: The number of refilling points.
- M: The number of users.
- W: The cost per unit of the resource.
- initial_resource: The number of units of resource available at time 0.
- T: The interval after which users require the resource again.
- users: A list of tuples where each tuple contains:
- demand_time: The initial time a user requires the resource.
- penalty: The penalty incurred if the user's demand is unmet.
- refills: A list containing the amount of resource added at each refilling point.
def minimum_total_cost(X: int, N: int, M: int, W: int, initial_resource: int, T: int, users: List[Tuple[int, int]], refills: List[int]) -> int:
# Your implementation here
# This function will implement the logic to calculate the minimum total cost
pass
example:
X = 19
N = 1
M = 4
W = 8
initial_resource = 7
refills = [10]
demands = [(1, 20), (2, 10), (4, 5), (6, 5)]
Output:
103
explanation:
The coach starts with initial_resource = 7.
At times 0, 1, 2, 4, and 6, the following users consume 1 unit of the resource:
Time 0: Driver consumes (remaining = 6)
Time 1: User 1 consumes (remaining = 5)
Time 2: User 2 consumes (remaining = 4)
Time 4: User 3 consumes (remaining = 3)
Time 6: User 4 consumes (remaining = 2)
At times 7 and 8:
Time 7: Driver consumes (remaining = 1)
Time 8: User 1 consumes (remaining = 0)
At time 9, User 2 needs a resource but it is not available, so they leave.
At time 10, 4 units are added at the first refilling point (remaining = 4).
At times 11, 13, 14, and 15:
Time 11: User 3 consumes (remaining = 3)
Time 13: User 4 consumes (remaining = 2)
Time 14: Driver consumes (remaining = 1)
Time 15: User 1 consumes (remaining = 0)
At time 18, User 3 needs a resource but it is not available, so they leave.
At time 19, the coach arrives at city O.
finally:
Total resource used = 11
Cost of resource = W * Total resource used = 8 * 11 = 88
Refund for Users 2 and 3 = C_2 + C_3 = 10 + 5 = 15
Total cost = 88 + 15 = 103
Output: 103
example:
X = 105
N = 3
M = 5
W = 9
initial_resource = 10
refills = [59, 68, 71]
demands = [(4, 71), (6, 32), (7, 29), (3, 62), (2, 35)]
output:
547
We discussed for 15 min, understanding the problem, then in silence, I thought for 10 min, and around 10 min, discussed with her on the approach, she was very insightful in my approach and corrected me at 2 places, basically asked.
solved it in O(N + M)
my solution
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#define LL long long
#define LD long double
using namespace std;
array<LL, 2> P[200000];
LL minVal[200000], preSum[200000];
LL dp[200000];
LD eps = 1e-8;
struct Line {
LL slope;
LL intercept;
};
LD intersect(Line l1, Line l2) {
return (LD)(l1.intercept - l2.intercept) / (l2.slope - l1.slope);
}
LL calculate(LL x, LL n, LL m, LL w, LL t, vector<LL> &D, LL *S) {
for (LL i = 0; i < m; ++i) {
minVal[i] = 1e18;
preSum[i] = (i ? preSum[i - 1] : 0) + P[i][1];
}
for (LL i = 0; i <= n; ++i) {
auto it = lower_bound(D.begin(), D.end(), S[i] % t);
if (it != D.begin()) {
it = prev(it);
minVal[it - D.begin()] = min(minVal[it - D.begin()], S[i] / t);
}
}
vector<Line> lines;
lines.push_back({w, 0});
for (LL i = 0; i < m; ++i) {
dp[i] = (i ? dp[i - 1] : 0) + (LL)((x - P[i][0]) / t + 1) * w;
if (minVal[i] != 1e18) {
LL left = 0, right = lines.size() - 1;
while (left < right) {
LL mid = (left + right + 1) / 2;
LD intersectVal = intersect(lines[mid - 1], lines[mid]);
if ((LD)minVal[i] - intersectVal >= -eps) {
left = mid;
} else {
right = mid - 1;
}
}
dp[i] = min(dp[i], lines[left].slope * minVal[i] + lines[left].intercept + minVal[i] * w * i + preSum[i]);
}
Line newLine = {-i * w, dp[i] - preSum[i]};
while (lines.size() >= 2) {
auto lastLine = lines.back();
auto sec lines[lines.size() - 2];
if (intersect(secondLastLine, lastLine) - intersect(secondLastLine, newLine) >= -eps) {
lines.pop_back();
} else {
break;
}
}
lines.push_back(newLine);
}
return dp[m - 1] + (x / t + 1) * w;
}
int main() {
LL x = 19, n = 1, m = 4, w = 8, t = 7;
vector<LL> D;
LL S[200001];
P[0] = {1, 20};
P[1] = {2, 10};
P[2] = {4, 5};
P[3] = {6, 5};
S[0] = 10;
S[1] = x;
for (LL i = 0; i < m; ++i) {
D.push_back(P[i][0]);
}
sort(S, S + n + 1);
cout << calculate(x, n, m, w, t, D, S) << '\n';
return 0;
}