Skip to main content
All Questions

Find the minimum fuel cost to travel from city A to city B.

Unlock detailed company stats for this questionUpgrade

Hackerland has g_nodes cities connected by g_edges bidirectional roads; road i connects g_from[i] to g_to[i] and needs g_weight[i] units of fuel. Vehicles have unlimited fuel capacity, and one unit of fuel in city i costs arr[i]; any amount can be bought in any city. Given cities A and B, return the minimum cost to travel from A to B, or -1 if impossible. Constraints: 2 <= g_nodes <= 200, 1 <= g_weight[i], arr[i] <= 10^6. Example: g_nodes=4, edges 1-2(w=3), 2-3(w=1), 2-4(w=7), arr=[3,6,2,2], A=1, B=4 -> 28. Example: g_nodes=5, edges 2-3(w=8), 4-1(w=4), 2-5(w=8), 5-3(w=2), arr=[9,5,9,6,3], A=2, B=4 -> -1. Approach: modified Dijkstra over (city, cheapest price seen) states.

Format · Microsoft SDE-2 HackerRank OA.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.