Car to Oasis with Gas Stations
Updated
Problem statement
A desert is a grid desert of text cells. Each cell is one of:
"c": the car's starting cell."o": the oasis the car wants to reach.".": sand, which the car can drive on."r": a rock, which the car cannot enter.- a positive whole number such as
"20": a gas station holding that many units of gas.
The car starts with gas units of gas in its tank. In one move it goes to the cell above, below, left or right. It cannot leave the grid. Each move costs 1 unit, so the car can make a move only when its tank holds at least 1 unit.
When the car enters a gas station, it takes all the gas there and adds it to its tank. A station gives its gas only the first time the car enters it; after that it is plain sand. The tank has no size limit. The car may arrive at a station or at the oasis with an empty tank.
Return true if the car can reach the oasis, and false otherwise.
Function
ride(desert: String[][], gas: int) → booleanExamples
Example 1
desert = [[".",".",".","o"],[".","r",".","r"],[".","r","r","r"],["c",".",".","20"]]gas = 2return = falseThe oasis is up and to the right of the car, and the only way there is the 6-move path up the left side and along the top. Two units of gas are not enough for that. The gas station 20 is 3 moves away (along the bottom row), and the car has only 2 units, so it cannot reach it either. The answer is false.
Example 2
desert = [[".",".",".","o"],[".","r",".","r"],[".","r","r","r"],["c",".",".","20"]]gas = 3return = trueWith 3 units the car drives along the bottom row to the station: 3 moves, tank empty on arrival. It collects the 20 units. Then it drives back along the bottom row and up the left side and along the top to the oasis: 9 moves. The tank has 20, so the answer is true.
Example 3
desert = [["c","3",".",".",".","o"]]gas = 1return = falseThe car moves onto the station (1 move, tank 0) and collects 3 units. The oasis is 4 moves from the station, but the tank has only 3. The station gives its gas only once, so the car cannot bounce back and forth to refill. The answer is false.
Example 4
desert = [["c","3",".",".",".","o"]]gas = 2return = trueThe car moves onto the station (1 move, tank 1) and collects 3 units, so the tank holds 4. The oasis is 4 moves away, and the tank is empty exactly on arrival. Arriving with an empty tank counts, so the answer is true.
Constraints
1 <= desert.length <= 151 <= desert[i].length <= 15, and every row has the same length.0 <= gas <= 100- Every cell is
"c","o",".","r", or a whole number from 1 to 50 written without leading zeros (a gas station). - The grid has exactly one
"c"and exactly one"o". - The grid has at most 8 gas stations.
More Bloomberg LP Onsite Interview questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Decode StringPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Replacements to Make Two Strings AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Currency Conversion RatePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Competitive ForagingAkuna Capital · OAAkuna Capital interview questions
- Escape the Haunted Castle with TreasuresPersona · OAPersona interview questions
- Swim in Rising WaterUiPath · PHONE SCREEN
- Shortest Grid Path with Obstacle EliminationsAmazon · ONSITE INTERVIEW
- Choose the Next Minesweeper ActionRunloop · PHONE SCREEN
- Shortest Grid Path with Obstacle EliminationGoogle · ONSITE INTERVIEWGoogle interview questions
- Shortest Distance from All BuildingsWaymo · PHONE SCREENWaymo interview questions
- Shortest Path in a Grid with Obstacles EliminationMercor · PHONE SCREENMercor interview questions
- Shortest Path Visiting All NodesInMobi · ONSITE INTERVIEWInMobi interview questions
- Sliding PuzzleNVIDIA · PHONE SCREENNVIDIA interview questions
Bloomberg LP interview experiences
- Bloomberg SWE Phone Screen and Onsite: 1 Experience
- Bloomberg SWE Phone Screens with Two Teams: 1 Experience
- Bloomberg AI Interview, Hiring Level Not Reported: 2 Experiences
- Bloomberg Experienced SWE AI Coding Interviews: 2 Experiences
- Bloomberg New Grad AI-Assisted Coding Rounds: 3 Experiences
- Bloomberg New Grad SWE AI-Assisted Coding Rounds: 1 Experience