Budget queries on a sorted price list, then a shortest path
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Microsoft's HackerRank assessment, last asked Aug 2025. It tests binary-search, prefix sums and shortest path.
Two problems, solved back to back in one timed assessment.
Part 1: how many items fit the budget
You get item prices sorted in non-decreasing order and a budget. Buy as many items as possible without going over the budget; with sorted prices the best choice is always a prefix of the list. A knapsack dynamic program works but is far too slow. Use prefix sums with binary search, so each budget query is O(log n).
Unless you agree on a different interface with the interviewer, assume:
max_items(prices: list[int], budget: int) -> int
prices = [1, 2, 3, 4, 5], budget = 10 -> 4 # 1 + 2 + 3 + 4 = 10
prices = [1, 2, 3, 4, 5], budget = 15 -> 5
prices = [1, 2, 3, 4, 5], budget = 0 -> 0
prices = [2, 3, 5, 8], budgets [1, 5, 9, 18] -> [0, 2, 2, 4]
Constraints: 0 <= len(prices) <= 10^5, 1 <= prices[i] <= 10^9, 0 <= budget <= 10^14.
Part 2: shortest path in a weighted graph
You get n nodes labelled 0..n-1 and an undirected edge list where each edge is [u, v, w] with a non-negative weight. Build the graph and return the length of the shortest path from source to target, or -1 if target cannot be reached. The distance from a node to itself is 0.
Unless you agree on a different interface with the interviewer, assume:
shortest_path(n: int, edges: list[list[int]], source: int, target: int) -> int
n = 5, edges = [[0,1,4],[0,2,1],[2,1,2],[1,3,1],[2,3,5]], source = 0, target = 3 -> 4 # 0-2-1-3
n = 4, edges = [[0,1,1],[1,2,1],[2,3,1]], source = 0, target = 3 -> 3
Constraints: 1 <= n <= 10^4, 0 <= len(edges) <= 10^5, 0 <= w <= 10^6.
Clarifying questions
- Part 1: are prices always sorted? Are there many budget queries for one list?
- Part 2: directed or undirected? Can weights be zero or negative? Can there be parallel edges?
- Should I return the path itself or only its length?
What a strong answer covers
- Part 1: why taking the cheapest items first is optimal; a prefix-sum array and
bisect_right(prefix, budget) - 1; O(n) preprocessing and O(log n) per query; sums that exceed 32 bits. - Part 2: an adjacency list with both directions; Dijkstra with a min-heap, skipping stale entries; O((V + E) log V); why negative weights would break it.
- Clean parsing of the input format and tests for unreachable targets and
source == target.
Follow-ups
- Part 1: items are not sorted and there are many queries.
- Part 2: return the path, not only its length.
- Part 2: all weights are 0 or 1. What faster method applies?
Where it's asked
- Microsoft · HackerRank assessment · Last asked Aug 202590 min · Two problems in a locked full-screen browser
What it tests
binary-search · prefix sums · shortest path
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
