Interview questions · Uber
Solve three algorithmic optimization and search problems
This multi-part question evaluates algorithmic problem-solving skills in combinatorial optimization and graph theory, covering task assignment under cardinality constraints (maximizing reward when exactly k tasks go to one worker) and directed-tree reorientation to create a single reachable root.
You are given three independent coding problems.
Problem 1: Allocate tasks between two workers to maximize reward
You have n independent tasks that must be split between two workers (worker 1 and worker 2). Every task must be done by exactly one of the two workers.
For each task (0-indexed or 1-indexed, as you prefer), you are given:
reward1[i]: the reward if worker 1 does taskreward2[i]: the reward if worker 2 does task
Additionally, worker 1 must do exactly k tasks in total (where ). The remaining tasks must be done by worker 2.
Task:
- Assign each of the tasks to one of the two workers such that:
- Worker 1 is assigned exactly tasks.
- Worker 2 is assigned the remaining tasks.
- Maximize the total reward, defined as the sum over all tasks of the reward from the worker to whom the task is assigned.
Input format (conceptual):
- Integer
n— number of tasks. - Integer
k— number of tasks that must be assigned to worker 1. - Array
reward1of lengthn. - Array
reward2of lengthn.
Output:
- A single number: the maximum possible total reward.
- (Optionally, you may also be asked to output which task indices are assigned to worker 1, but the core problem is computing the maximum total reward.)
You should design an algorithm that works efficiently for large (e.g., up to or ).
Problem 2: Reorient edges in a directed tree toward a root
You are given a directed graph with n nodes labeled from 1 to n and n - 1 directed edges. It is guaranteed that if you ignore edge directions, the underlying undirected graph is a tree (i.e., it is connected and acyclic). However, the current directions of the edges may be arbitrary, so the graph may not currently have a single node reachable from all others.
You are allowed to reverse the direction of any subset of edges. Reversing an edge counts as one operation.
You want to make the graph such that there exists some node r (1 ≤ r ≤ n) with the following property:
- For every node
x(includingr), there is a directed path fromxtorfollowing the directions of the (possibly reversed) edges.
In other words, after reorienting some edges, all nodes should be able to "flow" to a single root node r along directed paths.
Task:
- Over all possible choices of root node
r, determine the minimum number of edge reversals needed to achieve the property that every node has a directed path tor.
Input format (conceptual):
- Integer
n— number of nodes. n - 1directed edges, each given as an ordered pair(u, v)meaning there is an edge fromutov.
Output:
- A single integer: the minimum number of edges that must be reversed so that for some node
r, every node has a directed path tor. - (Optionally, you may also be asked to output at least one node
rthat achieves this minimum.)
Your algorithm should run in time close to linear in n (e.g., or ).
Problem 3: Find the leftmost column with a 1 in a sorted binary matrix
You are given a binary matrix A of size m × n ( rows, columns). Each cell is either 0 or 1. Each row is sorted in non-decreasing order, meaning:
- All the 0s in a row come before any 1s in that row.
- A typical row therefore looks like:
0 0 0 1 1 1(possibly all 0s or all 1s).
Task:
- Find the index of the leftmost column (smallest column index) that contains at least one
1in any row. - If the matrix contains no 1s at all, return
-1.
You may assume 0-based column indexing (return an integer in [0, n-1]) or 1-based indexing (return in [1, n]); just be clear and consistent in your implementation.
Input format (conceptual):
- Integers
m,n— number of rows and columns. - An
m × nmatrixAof 0s and 1s, where each row is sorted: all 0s, then all 1s.
Output:
- A single integer: the index of the leftmost column that contains at least one
1, or-1if no such column exists.
Complexity requirement:
- Design an algorithm that is asymptotically faster than the naive scan of the entire matrix in the worst case (for example, or ).
- Be prepared to explain your algorithm and analyze its time complexity.
Community answers
Nothing here yet.
Join in on CSWork →