FastPrepMaximum Gain on a Budgeted Returning Walk
Problems/Maximum Gain on a Budgeted Returning Walk

Maximum Gain on a Budgeted Returning Walk

NVIDIA logoNVIDIA● HardFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an NVIDIA onsite interview for a full-time role. The report asks to maximize node gain under an edge-cost budget while returning to the start. It mentions DFS. It leaves out whether the graph is directed, the rules for gaining again, the limits, and the interface. Here the graph is undirected with positive costs, and each vertex pays its gain once.

Our version is about 80% the same as the reported question.

An undirected graph has a nonnegative gain at each vertex and a positive cost on each edge. Start at vertex start, traverse zero or more edges, and finish at that same vertex with total edge cost at most budget. Return the greatest total gain possible.

  • The gain of start counts even if you do not move.
  • Each other vertex contributes its gain only on the first visit. Later visits contribute no additional gain.
  • Vertices and edges may be revisited. Parallel edges are allowed. Each row of edges is [u, v, cost] and may be used in either direction.

Function

maxGainWalk(gains: int[], edges: int[][], start: int, budget: int) → int

Examples

Example 1

gains = [5, 10, 7]edges = [[0, 1, 2], [1, 2, 2], [0, 2, 6]]start = 0budget = 8return = 22

The walk 0 to 1 to 2 to 1 to 0 costs 8 and gains 5 + 10 + 7. Revisiting 1 does not count it twice.

Example 2

gains = [4, 100, 3]edges = [[0, 1, 3], [0, 2, 1]]start = 0budget = 5return = 7

Node 1 needs cost 6 for a round trip; 0 to 2 to 0 costs 2 and gains 4 + 3.

Example 3

gains = [1, 7, 9, 100]edges = [[0, 1, 1], [0, 2, 1], [2, 3, 8]]start = 0budget = 4return = 17

Visit 1 and 2 on separate round trips through 0 for cost 4. Node 3 is too far away.

Example 4

gains = [3, 50]edges = [[0, 1, 1]]start = 0budget = 0return = 3

Staying at the starting node costs nothing and includes its gain.

Constraints

  • 1 <= gains.length <= 12; 0 <= gains[i] <= 1000.
  • 0 <= edges.length <= 100; each endpoint is a distinct valid vertex index and 1 <= cost <= 100.
  • 0 <= start < gains.length; 0 <= budget <= 1000.

More NVIDIA Onsite Interview questions

  • Check Endianness Of A Byte ArrayONSITE INTERVIEW · Seen Sep 2026
  • Implement MallocONSITE INTERVIEW · Seen Sep 2026
  • Implement PrintfONSITE INTERVIEW · Seen Sep 2026
  • Merge Two Arrays Into Sorted OrderONSITE INTERVIEW · Seen Sep 2026
  • Middle Node Of A Linked ListONSITE INTERVIEW · Seen Sep 2026
  • Ping Reply TrackingONSITE INTERVIEW · Seen Sep 2026
  • Reverse A StringONSITE INTERVIEW · Seen Sep 2026
  • Maximum Fish in a GridONSITE INTERVIEW · Seen Sep 2024
  • Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
  • First Register Overflow TickONSITE INTERVIEW · Seen May 2024

Similar questions at other companies

  • Minimum Path Cost with Edge DiscountsSkyscanner · ONSITE INTERVIEWSkyscanner interview questions
  • Cheapest Flight with Its PathAirbnb · OA · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
  • Cheapest Flights Within K StopsGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • All-Pairs Shortest PathsMorgan Stanley · OA · ONSITE INTERVIEWMorgan Stanley interview questions
  • Drone Delivery with Battery Swap StationsCisco · OACisco interview questions
  • Cities on Shortest Delivery RoutesMathWorks · OAMathWorks interview questions
  • Shortest Paths in a Nonnegative Directed GraphNation with NaMo · PHONE SCREEN
  • Cheapest Flights Within K StopsInMobi · OAInMobi interview questions
  • Parallel Courses IIISnowflake · ONSITE INTERVIEWSnowflake interview questions
  • Sum of Weighted Distances in a TreeGoogle · ONSITE INTERVIEWGoogle interview questions

NVIDIA interview experiences

  • NVIDIA Full-Time SWE Onsite Scheduling: 1 Experience
  • NVIDIA OA: What It Tests (2026)
  • NVIDIA DevOps, QA and Infra Intern Interviews: 12 Experiences
  • NVIDIA Ignite Internship OA Experience
  • NVIDIA Software Intern Interview Questions: 9 Experiences
  • NVIDIA Ignite SWE Intern OA Questions: 6 Experiences
All NVIDIA interview questionsSee NVIDIA hiring insights
Saved
class Solution {
    public int maxGainWalk(int[] gains, int[][] edges, int start, int budget) {
        // Write your code here.
    }
}
Checking account…