Uber Coding Questions and Answers

Uber Coding Questions with Solutions

Here on this page you will get Sample Uber coding questions and answers which will help you to prepare for Online Coding Assessment of Uber to hire freshers for SDE 1 Role.

Let you know that the Online Assessment will be conducted on CodeSignal Platform. After Application Process, candidates will be communicated through mail for Online Assessment on CodeSignal.

Sample Uber Coding Questions and Answers

Uber SDE 1 Profile Details

Role: Software Development Engineer 1 (SDE 1)
Work:

  • Develop, test, and maintain scalable software solutions.
  • Collaborate with teams to build high-performance applications.
  • Solve real-world engineering challenges using algorithms and data structures.

Tech Stack Required:

  • Programming Languages: Python, Java, C++, Go
  • Data Structures & Algorithms (DSA)
  • Databases: SQL, NoSQL (MongoDB, Cassandra)
  • Backend: Microservices, REST APIs
  • Frontend (if required): React, Angular
  • Cloud & DevOps: AWS, Kubernetes, Docker (preferred)

Checkout PrepInsta Prime:

Practice Coding Questions:

Further on this page you will get Sample Uber Coding Questions and Answers that will help you to prepare for Uber Online Coding Assessment on CodeSignal Platform.

Sample Uber Coding Questions with Solutions

Question 1: Finding Longest Repeating Character Replacement Problem

You are given a string s that contains only uppercase English letters and an integer k. Your task is to determine the length of the longest substring in s that can consist of just one distinct character after modifying the string.

You are allowed to make up to k replacements, where each replacement involves changing any character in the substring to another uppercase English letter.

The goal is to maximize the length of this uniform substring by carefully choosing which characters to replace while staying within the limit of k changes.

Constraints:

  • 1 <= s.length <= 1000
  • 0 <= k <= s.length

Solving Through Sliding Window Method

Maintain a sliding window and expand it to include more characters. If the number of characters to replace exceeds the limit, shrink the window from the left. Track the longest valid window during this process.

  • Time complexity: O(m*n)
  • Space complexity: O(m)

where n is the length of the string and m is the total number of unique characters in the string.

Code

class Solution {
public:
    int characterReplacement(std::string s, int k) {
        int res = 0;
        unordered_set charSet(s.begin(), s.end());

        for (char c : charSet) {
            int count = 0, l = 0;
            for (int r = 0; r < s.size(); r++) {
                if (s[r] == c) {
                    count++;
                }

                while ((r - l + 1) - count > k) {
                    if (s[l] == c) {
                        count--;
                    }
                    l++;
                }

                res = std::max(res, r - l + 1);
            }
        }
        return res;
    }
};

Question 2: Serializing and Deserializing Binary Tree.

Create an algorithm to convert a binary tree into a string (serialization) and then reconstruct the same binary tree from that string (deserialization).

Serialization means converting a binary tree into a format that can be saved or sent to another system. Deserialization means converting that saved format back into the original binary tree.

Goal is to ensure that the binary tree can be serialized into a string and later deserialized back to its original structure without any loss of information. There are no specific rules for how you should implement this; the approach is up to you.

Example:

Output of Example 1 of Good Node

Constraints:

  • 0 <= The number of nodes in the tree <= 1000.
  • -1000 <= Node.val <= 1000

Solving Breadth First Search Method

In the BFS approach, the binary tree is serialized level by level using a queue to store node values. During deserialization, the data is processed sequentially to rebuild the tree by connecting child nodes level-wise.

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */

class Codec {
public:

    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        if (!root) return "N";
        string res;
        queue<TreeNode*> queue;
        queue.push(root);

        while (!queue.empty()) {
            TreeNode* node = queue.front();
            queue.pop();
            if (!node) {
                res += "N,";
            } else {
                res += to_string(node->val) + ",";
                queue.push(node->left);
                queue.push(node->right);
            }
        }
        return res;
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        stringstream ss(data);
        string val;
        getline(ss, val, ',');
        if (val == "N") return nullptr;
        TreeNode* root = new TreeNode(stoi(val));
        queue<TreeNode*> queue;
        queue.push(root);

        while (getline(ss, val, ',')) {
            TreeNode* node = queue.front();
            queue.pop();
            if (val != "N") {
                node->left = new TreeNode(stoi(val));
                queue.push(node->left);
            }
            getline(ss, val, ',');
            if (val != "N") {
                node->right = new TreeNode(stoi(val));
                queue.push(node->right);
            }
        }
        return root;
    }
};

Question 3: N Queen Problem

The n-queens puzzle is about placing n queens on an n x n chessboard so that no two queens can attack each other.

Queens can attack in three ways: horizontally, vertically, and diagonally.

Your task is to find all possible ways to place the queens on the board, ensuring no two queens attack each other.

Each solution will show a unique board arrangement where ‘Q’ represents a queen and ‘.’ represents an empty square. The solutions can be returned in any order.

Example 1:

Example of N Queen problem

Constraints:

  • 1 <= n <= 8

Solving through Backtracking(Hash Set) Method

Here, we use hash sets to track the columns and diagonals already occupied by queens. These sets allow quick lookups to check if a cell is safe for placing a queen. The method reduces redundant checks and improves efficiency.

  • Time complexity: O(n!)
  • Space complexity: O(n^2)

Code

class Solution {
public:
    unordered_set<int> col;
    unordered_set<int> posDiag;
    unordered_set<int> negDiag;
    vector<vector<string>> res;

    vector<vector<string>> solveNQueens(int n) {
        vector<string> board(n, string(n, '.'));

        backtrack(0, n, board);
        return res;
    }

private:
    void backtrack(int r, int n, vector<string>& board) {
        if (r == n) {
            res.push_back(board);
            return;
        }

        for (int c = 0; c < n; c++) {
            if (col.count(c) || posDiag.count(r + c) || 
                negDiag.count(r - c)) {
                continue;
            }

            col.insert(c);
            posDiag.insert(r + c);
            negDiag.insert(r - c);
            board[r][c] = 'Q';

            backtrack(r + 1, n, board);

            col.erase(c);
            posDiag.erase(r + c);
            negDiag.erase(r - c);
            board[r][c] = '.';
        }
    }
};

Question 4: Partition Equal Subset Sum

Given an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise.

Example 1:

  • Input: nums = [1,2,3,5]
  • Output: false
  • Explanation: The array cannot be partitioned into equal sum subsets.

Constraints :

  • 1 <= nums.length <= 200
  • 1 <= nums[i] <= 100

Code:

class Solution {
public:
    bool canPartition(vector< int>& nums) {
        int sum = 0;
        for (int num : nums)
            sum += num;
        if (sum % 2 != 0)
            return false;
        int req = sum / 2;
        int n = nums.size();
        vector< bool> dp(req + 1, false);
        dp[0] = true;
        for (int i = 0; i < n; i++) {
            for (int j = req; j >= nums[i]; j--) {
                dp[j] = dp[j] || dp[j - nums[i]];
            }
        }
        return dp[req];
    }
};

Question 5: Coin Change II: Counting Distinct Combinations

  • This problem involves finding the number of distinct combinations of coins that sum up to a given target amount.
  • It is a classic example of a Dynamic Programming (DP) problem, focusing on combinations rather than permutations.

Problem Description : Coin Change 2

You are given:

  1. An integer array coins where each element represents the denomination of a coin (e.g., 1, 5, 10, etc.).
  2. An integer amount representing the target sum.

Objective: Return the number of distinct combinations of coins that sum up to amount. If it’s impossible to reach the amount, return 0.

Problem Breakdown

  1. Input: 2D grid of non-negative integers.
  2. Objective: Find the length of the longest strictly increasing path.
  3. Movement: Can move to adjacent cells (up, down, left, right) but not diagonally.
  4. Constraints: Each move must go to a cell with a strictly greater value.
  5. Goal: Determine the longest path by efficiently exploring the grid.

Constraints: 

  • 1 <= coins[i] <= 1000
  • 1 <= coins.length <= 100
  • 0 <= amount <= 1000

Code:

class Solution:
    def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
        ROWS, COLS = len(matrix), len(matrix[0])
        dp = {}  # (r, c) -> LIP

        def dfs(r, c, prevVal):
            if (r < 0 or r == ROWS or c < 0 or 
                c == COLS or matrix[r][c] <= prevVal
            ):
                return 0
            if (r, c) in dp:
                return dp[(r, c)]

            res = 1
            res = max(res, 1 + dfs(r + 1, c, matrix[r][c]))
            res = max(res, 1 + dfs(r - 1, c, matrix[r][c]))
            res = max(res, 1 + dfs(r, c + 1, matrix[r][c]))
            res = max(res, 1 + dfs(r, c - 1, matrix[r][c]))
            dp[(r, c)] = res
            return res

        for r in range(ROWS):
            for c in range(COLS):
                dfs(r, c, -1)
        return max(dp.values())

These were some of most Important Uber Coding Questions and Answers. If you want to practice more you can visit our coding blogs to explore more….and also visit PrepInsta Prime Course.

Faq's Related to Uber Hiring Process

Question 1. What types of coding questions are asked in the Uber hiring process?

Answer:

Uber Coding Questions mainly includes topics like arrays, strings, dynamic programming, graphs, and recursion.

Question 2. Which programming languages are allowed for the Uber coding test?

Answer:

  • Uber allows candidates to code in multiple languages, mainly including C++, Java and Python.

Question 3. How can I prepare for Uber coding round?

Answer:

To prepare effectively: