PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This multi-problem set evaluates broad algorithmic problem-solving skills, including bitwise reasoning, sliding-window and frequency analysis, stack/monotonic patterns, graph/DP reasoning with constrained jumps, and tree-path parity techniques.

  • medium
  • Uber
  • Coding & Algorithms
  • Software Engineer

Solve 12 coding interview problems

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

Below are multiple independent coding problems. --- ## Problem 1: Reduce an integer to 0 with \(\pm 2^i\) You are given a positive integer \(n\). In one operation you may replace \(n\) with \(n + 2^i\) or \(n - 2^i\) for any integer \(i \ge 0\). **Task:** Return the minimum number of operations needed to make \(n = 0\). **Constraints (typical):** \(1 \le n \le 10^{18}\). --- ## Problem 2: Shortest subarray with at least \(k\) distinct integers You are given an integer array `arr` of length \(n\) and an integer \(k\). A subarray is **good** if it contains **at least \(k\)** distinct values. **Task:** Return the length of the shortest good subarray. If no such subarray exists, return `-1`. **Constraints (typical):** \(1 \le n \le 2\times 10^5\). --- ## Problem 3: Apply the first valid discount to the right You are given an array `prices`. For each index `i`, define the discount as the first index `j > i` such that `prices[j] <= prices[i]`. - If such `j` exists: `final[i] = prices[i] - prices[j]` - Otherwise: `final[i] = prices[i]` (sold at full price) **Task:** Output: 1. The **sum** of all final prices. 2. The list of **0-based indices** that are sold at full price, in increasing order. **Constraints (typical):** \(1 \le n \le 2\times 10^5\). --- ## Problem 4: Max-score jump game with special prime jumps You are given an integer array `arr` of length \(n\). You start at index `0` and must end at index `n-1`. From index `i`, you may jump to: - `i + 1`, or - `i + p` where `p` is a **prime number** and `p % 10 == 3` (e.g., 3, 13, 23, 43, ...), and `i + p < n`. Your score is the sum of `arr` values on all visited indices (including start and end). **Task:** Return the maximum achievable score when reaching `n-1`. If `n-1` is unreachable, return `-1`. **Constraints (typical):** \(1 \le n \le 2\times 10^5\), `arr[i]` may be negative. --- ## Problem 5: Count ancestor endpoints whose path can be permuted into a palindrome You are given a rooted tree with nodes `0..n-1` (root is node `0`). Each node has a lowercase letter. Input is provided as: - `treeNodes = n` - `nodes`: a char array of length `n`, where `nodes[i]` is the character on node `i` - `nodeFrom`, `nodeTo`: arrays of length `n-1` describing directed parent→child edges (`nodeFrom[i] -> nodeTo[i]`), forming a tree - `queries`: array of start nodes For each query start node `u`: - Consider all endpoints `v` on the path from `u` up to the root `0` (i.e., `v` can be `u`, its parent, ..., `0`). - Let the multiset of characters on the path `u -> ... -> v` (inclusive) be collected. - This path is counted if its characters can be **rearranged** into a palindrome (i.e., at most one character has an odd frequency). **Task:** For each query `u`, return how many such endpoints `v` exist. **Constraints (typical):** \(n, q \le 2\times 10^5\). --- ## Problem 6: Maximize pipeline throughput under a scaling budget You have \(n\) services connected in a pipeline. The pipeline throughput is: \[ T = \min_i throughput[i] \] You may scale service `i` any number of times. Each scaling: - increases `throughput[i]` by `1` - costs `scale_cost[i]` Given integer `budget`, **total scaling cost** must be \(\le budget\). **Task:** Return the maximum achievable pipeline throughput `T`. **Constraints (typical):** \(n \le 2\times 10^5\), values up to \(10^{18}\). --- ## Problem 7: Elevator-then-stairs with energy-dependent stair time You must go from floor `0` to floor `N`. You may take the elevator first from floor `0` to floor `x` (choose `x` once, `0 <= x <= N`). - For each elevator floor ascended: gain `e1` energy and spend `t1` time. - After elevator: your energy is `E = x * e1`. Then you must climb the remaining `N - x` floors using stairs. For each stair floor: - At the **start** of the floor, if current energy is `E`, the time spent is `ceil(c / E)`. - Then you spend `e2` energy: `E := E - e2`. - Energy may never become negative; if `E < 0` at any step, that choice of `x` is invalid. **Task:** Choose `x` to minimize total time. If no `x` is feasible, return `-1`. **Constraints (typical):** \(1 \le N \le 10^6\), parameters are positive integers. --- ## Problem 8: Assign tasks to two people with exactly \(k\) tasks for person A You have `n` tasks. If task `i` is done by: - person A: reward `reward1[i]` - person B: reward `reward2[i]` Each task must be assigned to exactly one person. Person A must do **exactly `k` tasks**. **Task:** Return the maximum possible total reward. **Constraints (typical):** \(n \le 2000\), \(0 \le k \le n\). --- ## Problem 9: Score from each start index with energy thresholds You are given: - `layers[0..n-1]`: energy cost to attempt/pass level `j` - `energyReq[0..n-1]`: minimum remaining energy needed *after paying the cost* to pass level `j` - initial energy `K` Starting from index `i`: - Set `E = K`. - For `j = i..n-1`: 1. Pay the level cost: `E := E - layers[j]`. If `E < 0`, stop. 2. If `E >= energyReq[j]`, you pass and gain 1 point; otherwise stop (cannot proceed further). **Task:** Return an array `score` of length `n` where `score[i]` is the number of points you can earn starting at level `i`. **Note:** An \(O(n^2)\) solution will time out; design a faster approach. --- ## Problem 10: Buy the maximum number of consecutive items per query You are given `prices[0..n-1]` (positive integers). Each query provides: - `pos` (1-based starting index) - `amount` (budget) From index `start = pos - 1`, you can buy items consecutively to the right while the running sum does not exceed `amount`. **Task:** For each query, return the maximum number of items you can buy. **Constraints (typical):** \(n, q \le 2\times 10^5\). --- ## Problem 11: Minimum edge reversals so every node can reach the chosen root You are given `n` nodes and `n-1` directed edges `edges[i] = [a, b]` meaning `a -> b`. Ignoring direction, the graph is a tree. For each node `r` considered as the root, you may reverse any subset of edges. **Task:** Compute `res[r]` = the minimum number of edge reversals required so that **every node has a directed path to `r`** (equivalently, all edges point toward `r` along the tree). Return `res` for all `r`. **Constraints (typical):** \(n \le 2\times 10^5\). --- ## Problem 12: For each \(w\), do values \(1..w\) occupy a contiguous block? You are given a permutation `perm[0..n-1]` containing each integer from `1` to `n` exactly once. For each window size \(w = 1..n\), define `ans[w] = 1` if the positions of the numbers `{1,2,...,w}` in `perm` form a contiguous subarray (in any order). Otherwise, `ans[w] = 0`. Equivalently, let `pos[x]` be the index where value `x` appears. Then `ans[w] = 1` iff: \[ \max_{1\le x\le w} pos[x] - \min_{1\le x\le w} pos[x] + 1 = w. \] **Task:** Return `ans[1..n]`. **Constraints (typical):** \(n \le 2\times 10^5\).

Quick Answer: This multi-problem set evaluates broad algorithmic problem-solving skills, including bitwise reasoning, sliding-window and frequency analysis, stack/monotonic patterns, graph/DP reasoning with constrained jumps, and tree-path parity techniques.

Part 1: Reduce an Integer to 0 with +/- Powers of Two

You are given a non-negative integer n. In one operation, you may replace n with n + 2^i or n - 2^i for any integer i >= 0. Return the minimum number of operations needed to make n equal to 0.

Constraints

  • 0 <= n <= 10^18

Examples

Input: (0,)

Expected Output: 0

Explanation: Already at 0, so no operations are needed.

Input: (1,)

Expected Output: 1

Explanation: Use -1 once.

Input: (3,)

Expected Output: 2

Explanation: One optimal sequence is 3 -> 4 -> 0.

Input: (39,)

Expected Output: 3

Explanation: For example: 39 -> 40 -> 32 -> 0.

Hints

  1. If n is even, think about factoring out a power of 2.
  2. If n is odd, your first move must make it even: compare n - 1 and n + 1.

Part 2: Shortest Subarray with At Least k Distinct Integers

Given an integer array arr and an integer k, find the length of the shortest contiguous subarray that contains at least k distinct values. If no such subarray exists, return -1.

Constraints

  • 0 <= len(arr) <= 2 * 10^5
  • 1 <= k <= 2 * 10^5

Examples

Input: ([], 1)

Expected Output: -1

Explanation: There is no subarray at all.

Input: ([1], 1)

Expected Output: 1

Explanation: The single element subarray already has 1 distinct value.

Input: ([1, 2, 1, 3, 4], 3)

Expected Output: 3

Explanation: One shortest good subarray is [2, 1, 3].

Input: ([1, 1, 1], 2)

Expected Output: -1

Explanation: The array contains only one distinct value.

Hints

  1. A sliding window can track how many distinct values are inside the current range.
  2. Once a window has at least k distinct numbers, try shrinking it from the left.

Part 3: Apply the First Valid Discount to the Right

For each price at index i, find the first index j > i such that prices[j] <= prices[i]. If such j exists, the final price is prices[i] - prices[j]; otherwise, the item is sold at full price. Return both the sum of all final prices and the list of 0-based indices sold at full price.

Constraints

  • 0 <= len(prices) <= 2 * 10^5
  • 1 <= prices[i] <= 10^9

Examples

Input: ([],)

Expected Output: (0, [])

Explanation: No items means total 0 and no full-price indices.

Input: ([8, 4, 6, 2, 3],)

Expected Output: (15, [3, 4])

Explanation: Final prices are [4, 2, 4, 2, 3].

Input: ([1, 2, 3, 4],)

Expected Output: (10, [0, 1, 2, 3])

Explanation: No item gets a discount.

Input: ([10, 1, 1, 6],)

Expected Output: (16, [2, 3])

Explanation: Final prices are [9, 0, 1, 6].

Hints

  1. The phrase first smaller-or-equal value to the right suggests a monotonic stack.
  2. When a new price resolves discounts for earlier items, update the total immediately.

Part 4: Max-Score Jump Game with Special Prime Jumps

You are given an integer array arr. Start at index 0 and reach index n - 1. From index i, you may jump to i + 1, or to i + p where p is a prime number ending in digit 3 and i + p < n. Your score is the sum of arr values at all visited indices, including the start and end. Return the maximum achievable score. In this version, n is small enough for dynamic programming over all valid jump lengths.

Constraints

  • 0 <= len(arr) <= 5000
  • -10^9 <= arr[i] <= 10^9

Examples

Input: ([],)

Expected Output: -1

Explanation: There is no start or end index.

Input: ([7],)

Expected Output: 7

Explanation: Start and end are the same index.

Input: ([5, -100, 4, 10],)

Expected Output: 15

Explanation: Jump directly from index 0 to index 3 using length 3.

Input: ([5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 20],)

Expected Output: 25

Explanation: A jump of length 13 skips all the negative values.

Hints

  1. Precompute all allowed jump lengths less than n with a sieve.
  2. Let dp[i] be the best score for reaching index i.

Part 5: Count Ancestor Endpoints Whose Path Can Be Rearranged into a Palindrome

You are given a rooted tree with root 0. Each node has a lowercase letter. For each query node u, count how many ancestor endpoints v on the path from u up to 0 make the path u -> ... -> v have character counts that can be rearranged into a palindrome.

Constraints

  • 1 <= treeNodes <= 2 * 10^5
  • 1 <= len(queries) <= 2 * 10^5
  • nodes[i] is a lowercase English letter

Examples

Input: (5, ['a', 'b', 'a', 'c', 'b'], [0, 0, 1, 1], [1, 2, 3, 4], [4, 3, 2, 0])

Expected Output: [3, 1, 2, 1]

Explanation: For node 4, all three ancestor endpoints 4, 1, and 0 are valid.

Input: (4, ['a', 'b', 'a', 'b'], [0, 1, 2], [1, 2, 3], [3, 2])

Expected Output: [3, 2]

Explanation: On the chain, node 3 has three valid ancestor endpoints.

Input: (1, ['z'], [], [], [0])

Expected Output: [1]

Explanation: The only path is the single node itself.

Hints

  1. A multiset can be rearranged into a palindrome iff at most one character has odd frequency.
  2. Track parity with a 26-bit mask and count ancestor-prefix masks along the current DFS path.

Part 6: Maximize Pipeline Throughput Under a Scaling Budget

A pipeline has n services, and the overall throughput is the minimum value in the throughput array. You may increase throughput[i] by 1 any number of times, and each unit increase costs scale_cost[i]. Given a total budget, return the maximum achievable pipeline throughput.

Constraints

  • 1 <= len(throughput) <= 2 * 10^5
  • 1 <= throughput[i], scale_cost[i], budget <= 10^18

Examples

Input: ([4, 2, 7], [3, 1, 2], 5)

Expected Output: 4

Input: ([1, 1, 1], [2, 2, 2], 6)

Expected Output: 2

Input: ([5], [10], 100)

Expected Output: 15

Input: ([3, 8, 6], [1, 2, 3], 0)

Expected Output: 3

Input: ([10, 10], [1, 1], 1)

Expected Output: 10

Input: ([2, 2, 2], [1, 1, 1], 9)

Expected Output: 5

Input: ([1000000000000000000], [1], 1000000000000000000)

Expected Output: 2000000000000000000

Hints

  1. If you can achieve throughput T, then every smaller target is also achievable.
  2. Binary search the answer and write a function that computes the cost of reaching a target minimum.

Part 7: Elevator-Then-Stairs with Energy-Dependent Stair Time

You must go from floor 0 to floor N. First, choose a single elevator ride from floor 0 to floor x. Each elevator floor gives e1 energy and costs t1 time. Then climb the remaining floors by stairs. For each stair floor, if your current energy is E, the time spent is ceil(c / E), then your energy decreases by e2. Choose x to minimize total time. In this version, N is small enough to try every split.

Constraints

  • 0 <= N <= 4000
  • 1 <= e1, t1, e2, c <= 10^9

Examples

Input: (0, 5, 2, 3, 10)

Expected Output: 0

Explanation: No movement is required.

Input: (3, 5, 2, 2, 10)

Expected Output: 5

Explanation: Best choice is x = 2, giving total time 4 + 1 = 5.

Input: (4, 1, 5, 1, 3)

Expected Output: 15

Explanation: Best choice is x = 2.

Input: (1, 1, 10, 5, 5)

Expected Output: 10

Explanation: Going fully by elevator is the only feasible option.

Hints

  1. Try every possible elevator stop x from 0 to N.
  2. A choice is feasible only if the starting elevator energy is enough to pay for all remaining stair floors.

Part 8: Assign Tasks to Two People with Exactly k Tasks for Person A

There are n tasks. If task i is assigned to person A, you earn reward1[i]. If assigned to person B, you earn reward2[i]. Every task must be assigned to exactly one person, and person A must receive exactly k tasks. Return the maximum total reward.

Constraints

  • 1 <= len(reward1) == len(reward2) <= 2000
  • 0 <= k <= n
  • 0 <= reward1[i], reward2[i] <= 10^9

Examples

Input: ([8, 7, 15], [5, 10, 5], 2)

Expected Output: 33

Explanation: Choose tasks 0 and 2 for person A.

Input: ([3, 1, 2], [4, 5, 6], 0)

Expected Output: 15

Explanation: All tasks go to person B.

Input: ([3, 1, 2], [4, 5, 6], 3)

Expected Output: 6

Explanation: All tasks must go to person A.

Input: ([10], [1], 1)

Expected Output: 10

Explanation: The single task goes to person A.

Hints

  1. Start by assigning every task to person B.
  2. Switching task i from B to A changes the total by reward1[i] - reward2[i].

Part 9: Score from Each Start Index with Energy Thresholds

You are given arrays layers and energyReq, and an initial energy K. Starting from index i, you process levels from i to the end. For each level j, first subtract layers[j] from energy. If energy becomes negative, you stop. Otherwise, if the remaining energy is at least energyReq[j], you gain 1 point and continue; if not, you stop. Return score[i] for every start index i.

Constraints

  • 0 <= len(layers) == len(energyReq) <= 2 * 10^5
  • 0 <= layers[i], energyReq[i], K <= 10^18

Examples

Input: ([], [], 10)

Expected Output: []

Explanation: No levels means no scores.

Input: ([2, 1, 3], [1, 2, 0], 4)

Expected Output: [1, 2, 1]

Explanation: Starting at index 1 lets you pass levels 1 and 2.

Input: ([5, 1], [0, 0], 4)

Expected Output: [0, 1]

Explanation: Starting at 0 fails immediately because the cost is too high.

Input: ([1, 2, 1, 1], [2, 1, 0, 0], 4)

Expected Output: [3, 3, 2, 1]

Explanation: The first failure from index 0 happens only at the last level.

Hints

  1. Rewrite the pass condition using prefix sums.
  2. For each i, you need the first j >= i where a precomputed threshold exceeds prefix[i].

Part 10: Buy the Maximum Number of Consecutive Items per Query

You are given positive prices and multiple queries. Each query gives a 1-based starting position pos and a budget amount. Starting from pos, buy consecutive items to the right while the running total does not exceed the budget. Return the maximum number of items that can be bought for each query.

Constraints

  • 0 <= len(prices), len(queries) <= 2 * 10^5
  • 1 <= prices[i], amount <= 10^18

Examples

Input: ([2, 1, 3, 2, 4], [(1, 3), (2, 6), (5, 10)])

Expected Output: [2, 3, 1]

Explanation: From position 2 with budget 6, you can buy 1 + 3 + 2.

Input: ([5], [(1, 4), (1, 5)])

Expected Output: [0, 1]

Explanation: The first budget is too small; the second buys exactly one item.

Input: ([3, 3, 3], [(2, 2), (2, 3), (1, 9)])

Expected Output: [0, 1, 3]

Explanation: The full budget 9 buys all three items from the start.

Hints

  1. Because prices are positive, prefix sums are strictly increasing.
  2. For each query, use binary search on the prefix sum array.

Part 11: Minimum Edge Reversals so Every Node Can Reach the Chosen Root

You are given n nodes and n - 1 directed edges that form a tree if directions are ignored. For every possible root r, compute the minimum number of edge reversals needed so that every node has a directed path to r.

Constraints

  • 1 <= n <= 2 * 10^5
  • len(edges) == n - 1

Examples

Input: (1, [])

Expected Output: [0]

Explanation: A single node needs no reversals.

Input: (3, [[0, 1], [2, 1]])

Expected Output: [1, 0, 1]

Explanation: Root 1 already has all edges effectively pointing toward it.

Input: (4, [[0, 1], [0, 2], [0, 3]])

Expected Output: [3, 2, 2, 2]

Explanation: With root 0, all three edges must be reversed.

Hints

  1. First compute the answer for one root, such as 0.
  2. When rerooting across an edge, the answer changes by exactly 1 in one direction or the other.

Part 12: For Each w, Do Values 1..w Occupy a Contiguous Block?

You are given a permutation perm of the integers 1 through n. For every w from 1 to n, determine whether the positions of the values 1, 2, ..., w form a contiguous subarray in perm. Return an array of 0/1 answers.

Constraints

  • 0 <= len(perm) <= 2 * 10^5
  • perm is a permutation of 1..n

Examples

Input: ([],)

Expected Output: []

Input: ([1],)

Expected Output: [1]

Input: ([4, 1, 3, 2],)

Expected Output: [1, 0, 1, 1]

Input: ([2, 1, 4, 3, 5],)

Expected Output: [1, 1, 0, 1, 1]

Input: ([1, 2, 3, 4],)

Expected Output: [1, 1, 1, 1]

Input: ([5, 4, 3, 2, 1],)

Expected Output: [1, 1, 1, 1, 1]

Input: ([2, 4, 1, 3],)

Expected Output: [1, 0, 0, 1]

Hints

  1. Store the position of each value.
  2. As w grows from 1 to n, update only the current minimum and maximum position.
Last updated: Apr 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Thread-Safe Token-Bucket Rate Limiter - Uber (medium)
  • Group Anagrams - Uber (medium)
  • Quadtree for 2D Geospatial Points - Uber (medium)
  • Deep Equality of Two Records - Uber (medium)
  • Shortest Path in a Grid with Blocked Cells - Uber (medium)