Pick the most profitable repeated query
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Snowflake's two coding screens round, last asked Jan 2026. It tests arrays, greedy and math.
A data warehouse has a time budget of k minutes and n query types. Query type i takes times[i] minutes per run and earns profits[i] per completed run.
You must commit to one query type and run it back to back, as many whole runs as fit in k minutes. A run that would end after k minutes earns nothing and cannot be started.
Implement:
def max_profit_query(times: list[int], profits: list[int], k: int) -> list[int]:
...
Return [best_total_profit, index] for the query type with the highest total profit. If several types tie, return the smallest index. If no type can run even once within k minutes, return [0, -1].
Constraints
-
0 <= n <= 10^5,len(times) == len(profits) -
1 <= times[i] <= 10^9,0 <= profits[i] <= 10^9 0 <= k <= 10^12
Example 1
times = [6, 5], profits = [7, 5], k = 10
Output: [10, 1]
Type 0 fits once (7). Type 1 fits twice (10). Type 0 earns more per minute, but its leftover 4 minutes are wasted.
Example 2
times = [2, 4, 1], profits = [4, 8, 2], k = 8
Output: [16, 0]
Every type earns 16, so the smallest index wins.
Example 3
times = [11, 20], profits = [100, 5], k = 10
Output: [0, -1]
Clarifying questions
- If a query type fits but earns
0, is the answer[0, index]or[0, -1]? - Can
k // times[i] x profits[i]overflow? (Up to about10^21, which is beyond 64 bits.) - Are the times always at least 1?
- Can we switch query types partway? (Not in this version.)
What a strong answer covers
- For each type that fits at least once, computes
(k // times[i]) x profits[i]and keeps the best, with the smallest index on ties. That is O(n) time and O(1) memory. - Points out the overflow: Python is fine, but Java or C++ need 128-bit math or a careful comparison.
- Handles
n = 0,k = 0, and no type fitting. - Tests all three examples and a tie between several types.
Follow-ups
- Any mix of query types: unbounded knapsack, and why it is slow for
k = 10^12. - Each type can run at most
c[i]times. - Many budgets
kare asked. - The earnings drop after each run of the same type.
Where it's asked
- Snowflake · Two coding screens · Last asked Jan 202660 min · Two back-to-back live coding rounds, often on the same day; some loops pair coding with system design · Varies: some screens forbid AI, some 2026 screens allow it. The scheduling email says.
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
