Uber's Online Test Experience

Last Updated : 23 Jul, 2025

Online test problems excite some people and make others very nervous. A fascinating challenge popped up in the Uber online test: Participants had to find the shortest path from multiple taxi stands to a passenger within a grid. This task taught lessons not just about designing algorithms, but also about optimizing routes in real-world situations.

Problem Statement

Let's broaden the scope and say you need to find the shortest distance between all available taxi stands and a traveler's location on a grid. Picture the grid as a 2D matrix where each cell contains either a 0(road) or 1(building). You can move along the road so your goal is to calculate the distance to the closest taxi stand from where the traveler is. The taxi stands and passengers are always on the road(0).

Input:

A 2D list grid of size n x m.

A list taxi of pairs (x, y) representing the locations of taxi stands.

A pair (x, y) represents the location of the passenger.

Output:

The shortest path from the nearest taxi stand to the passenger. The path should be represented as a list of coordinate pairs from start to destination. If no path is found, return an empty list.

Example1:

Grid:

0 0 0

0 1 0

0 0 0

Taxi Stands: [(0, 0)]

Passenger: (2, 2)

Expected Output: Shortest path (0, 0),(1, 0),(2, 0),(2, 1),(2, 2) or (0, 0),(0, 1),(0, 2),(1, 2),(2, 2).

Example2:
Grid:

0 1 0 0

0 0 0 0

0 1 1 1

0 0 0 0

Taxi Stands: [(0, 0), (3, 3)]

Passenger: (1, 3)

Expected Output: Shortest path (0, 0),(1, 0),(1, 1),(1, 2),(1, 3).

Solution Approach:

I used a Breadth-First Search to solve this problem. BFS works well for problems on unweighted grids because it explores level by level, which leads to finding the shortest path. Here's how I tackled this back-to-bedroom problem:

1. **Initialization**: The BFS from each taxi stand helps to find which one has the shortest path to the passenger. This process sets up the distance matrix and parent pointers for the shortest paths.

2. **Run BFS**: Starting from the taxi stand, we look at all possible moves - up down, left, and right. We update distances and parent pointers as we go until we find the passenger or run out of paths to check.

3. **Reconstruct Path:** When you run BFS from any taxi stand, it's easy to rebuild the path from the stand to the passenger using parent pointers. This step checks if a new path found this way is shorter than the current best path. If so, it updates the shortest path.

Selection: After processing all taxi stands, the algorithm picks the shortest path from all paths discovered during the run.

Here's a way to code the solution in C++:

C++
#include <bits/stdc++.h>
using namespace std;

// BFS function to find the shortest path
vector<pair<int, int>> bfs(vector<vector<int>> &grid, pair<int, int> start, pair<int, int> end)
{
    int n = grid.size();
    int m = grid[0].size();
    vector<vector<int>> dist(n, vector<int>(m, INT_MAX));
    vector<vector<pair<int, int>>> parent(n, vector<pair<int, int>>(m, {-1, -1}));
    vector<pair<int, int>> path;
    queue<pair<int, int>> q;

    // Initialize
    q.push(start);
    dist[start.first][start.second] = 0;

    // Directions for moving in the grid
    vector<pair<int, int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

    while (!q.empty())
    {
        auto h = q.front();
        auto x = h.first;
        auto y = h.second;
        q.pop();

        // If reached destination
        if (x == end.first && y == end.second)
        {
            while (x != -1 && y != -1)
            {
                path.push_back({x, y});
                tie(x, y) = parent[x][y];
            }
            reverse(path.begin(), path.end());
            return path;
        }

        for (auto d : directions)
        {
            int nx = x + d.first;
            int ny = y + d.second;
            if (nx >= 0 && nx < n && ny >= 0 && ny < m && grid[nx][ny] == 0 && dist[nx][ny] == INT_MAX)
            {
                dist[nx][ny] = dist[x][y] + 1;
                parent[nx][ny] = {x, y};
                q.push({nx, ny});
            }
        }
    }

    return path; // Return empty path if no path is found
}

vector<pair<int, int>> findPath(vector<vector<int>> grid, vector<pair<int, int>> taxi,
                                pair<int, int> passenger)
{
    vector<pair<int, int>> shortestPath;
    int minDist = INT_MAX;

    for (auto &stand : taxi)
    {
        vector<pair<int, int>> path = bfs(grid, stand, passenger);
        if (!path.empty() && path.size() < minDist)
        {
            minDist = path.size();
            shortestPath = path;
        }
    }

    if (shortestPath.empty())
    {
        cout << "No path found" << endl;
    }

    return shortestPath;
}

int main()
{
    vector<vector<int>> grid = {{0, 1, 0, 0}, {0, 0, 0, 0}, {0, 1, 1, 1}, {0, 0, 0, 0}};
    vector<pair<int, int>> taxi = {{0, 0}, {3, 3}};
    pair<int, int> passenger = {1, 3};

    vector<pair<int, int>> ans = findPath(grid, taxi, passenger);

    if (!ans.empty())
    {
        for (auto x : ans)
        {
            cout << x.first << " " << x.second << endl;
        }
    }

    return 0;
}

Output
0 0
1 0
1 1
1 2
1 3

The issue I've included is easy to grasp. The code isn't complicated so it's simple to set up.

  1. Multiple Stand Management: These help run several BFS calls. This could boost productivity for bigger grids or more stands.
  2. Grid Obstacles and Boundaries: When using the BFS algorithm, we need to pay attention to grid edges and obstacle points.
  3. Path Reconstruction: Rebuilding the route from the end to the start helped us understand BFS and track the path.

Similar Problems:

Shortest path in grid with obstacles

Min Cost Path

It felt tough to tackle this shortest path problem during the online Uber test. Later after test I got the hang of how BFS works with these grid-based problems. Another key thing about this challenge is how well it handles multiple start points and obstacles in path finding. This experience has been super helpful boosting my problem-solving skills, which has set me up for future coding challenges.


Comment