← Uber Interview Insights

Uber·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
Jul 2026

Summary

Uber SWE online assessment, 90 minutes, two algorithmic problems. Both were harder than I expected for an OA format, and the tree one in particular took me a while to even understand what was being asked.

Questions Asked (2)

Q1

Given a permutation of integers 1 through N, for each prefix of values {1, 2, ..., k}, determine whether those values occupy a contiguous segment in the array. Return a binary string of length N encoding the answer for each k.

Algorithms & Data Structures
Author's notes

I spent way too long on edge cases before I even had a working solution.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Track the minimum and maximum positions of values 1..k as you iterate through the permutation. For each k, the values occupy a contiguous segment if and only if maxPos - minPos + 1 == k. Build the binary string by checking this condition at each step.

Pro tip: Mention that this O(N) solution is optimal because you must read the entire permutation, and explicitly state the invariant: the set of positions is contiguous exactly when its size equals the range span. This shows you understand the underlying principle, not just the algorithm.

1. Clarify and define the problem

Confirm that the permutation contains integers 1 through N exactly once, and that for each k from 1 to N, we need to check if the positions of values 1..k form a contiguous block. The output is a string of '1's and '0's of length N.

2. Identify the key condition

Realize that a set of k distinct positions is contiguous if and only if the difference between the maximum and minimum positions is exactly k-1. Equivalently, maxPos - minPos + 1 == k.

3. Design an efficient algorithm

Iterate through the permutation once, maintaining the minimum and maximum positions seen so far for values 1..k. At each step k, check the condition and append '1' or '0' to the result string.

4. Analyze complexity and edge cases

The algorithm runs in O(N) time and O(N) space (for the position array and output). Edge cases include k=1 (always contiguous) and k=N (always contiguous).

5. Test with examples

Walk through a small example, such as permutation [2,1,3], to verify the logic: for k=1, positions of {1} is index 1 (0-based), contiguous; for k=2, positions of {1,2} are indices 0 and 1, contiguous; for k=3, all indices, contiguous. Output '111'.

Key Points to Mention

  • The condition for contiguity: maxPos - minPos + 1 == k.
  • Single pass O(N) time complexity and O(N) space for the position array.
  • Maintaining running min and max positions as k increases.
  • The output is a binary string of length N, built incrementally.
  • Edge cases: k=1 and k=N are always contiguous.
  • Alternative approach: using a segment tree or union-find, but the min-max method is simpler and optimal.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

You are given a directed graph on N nodes with N-1 edges that forms an undirected tree. Find the minimum number of edge reversals needed so that all edges point away from some chosen root, minimized over all possible root choices.

Algorithms & Data Structures
Author's notes

This one genuinely stumped me at first.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as finding a root that minimizes the number of edges needing reversal to point away from it. Use two DFS passes: first compute the cost for an arbitrary root, then reroot to compute costs for all nodes in O(N) time by tracking how the cost changes when moving the root across an edge.

Pro tip: Mention that the answer is the minimum over all roots of (N-1 - number of edges already pointing away from the root), and emphasize that the rerooting technique generalizes to many tree DP problems.

1. Understand the problem and define cost

Clarify that for a fixed root, an edge must be reversed if it points toward the root instead of away. The cost is the count of such edges.

2. Compute cost for an arbitrary root

Run a DFS from node 0 (or any node) to count how many edges point away from it. This gives the reversal count for that root.

3. Reroot to compute costs for all nodes

Use a second DFS to propagate the cost to children. When moving the root from u to v, the cost changes by +1 if edge u->v exists (since it now points toward the new root), else -1.

4. Find the minimum cost

Track the minimum cost across all nodes during the rerooting pass and return it as the answer.

Key Points to Mention

  • Tree rerooting technique for O(N) time complexity
  • Directed edges and reversal condition: edge must point away from root
  • Cost change when moving root across an edge: +1 if edge direction is from old root to new root, else -1
  • Two-pass DFS: first to compute initial cost, second to propagate
  • Handling of large N: O(N) time and O(N) space
  • Edge cases: N=1 (no edges, answer 0), star graphs, path graphs

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.