Uber SWE OA Question Sharing: Complete Solutions and Code for Two Full HackerRank Dynamic Programming Problems

86 Views
No Comment

Just done Uber SWE OA (March 26th), 90 minutes on HackerRank, consisting of two questions. In a nutshell: Both problems are Hard-level DP questions, significantly more challenging than regular OA problems. The time constraints were particularly tight! Especially the second problem involving changing nodes DP; it can be easily stalled if one has not seen similar question types before. Below, I share the complete problem statement along with the solution approach for those who are preparing to join Uber.

Uber SWE OA Question Sharing: Complete Solutions and Code for Two Full HackerRank Dynamic Programming Problems

Title 1: Prime Jumps

Problem Description Starting from square 0, with an initial score of 0. Each move can:

  • Go right. 1 Step, or
  • Go right. I apologize, but you have provided incomplete text. Could you please provide the full Chinese sentence or text you would like translated? Step by step, where p is a prime number ending in 3 (e.g., 3, 13, 23), add the score of cell[i]. Find the maximum possible score to reach cell[n-1].

Example Cell = [0, -10, -20, -30, 50]; Maximum score = 40 (path: 0 → 1 → 4)

ConstraintsFor n ≤ 10⁴, cell[i] ∈ [-10⁴, 10⁴]

Approach to Solving the Problem Classic DP with Prime Preprocessing

  • First, use the Sieve of Eratosthenes to identify all prime numbers less than or equal to \(n-1 \) that have a units digit of 3.
  • Dp[i] represents the maximum score to reach cell i.
  • Transfer: dp[i] = max(dp[i - 1] + cell[i], ∑ dp[i - p] + cell[i] for all valid p)

Reference Code (Python)

Python

Def maxGameScore(cell):
    n = len(cell)
    if n == 1:
        return cell[0]

    # Sieve of Eratosthenes: Find all prime numbers = p and dp[i-p] != INF:
                dp[i] = max(dp[i], dp[i-p] + cell[i])

    return dp[n-1]

# Testing
cell = [0, -10, -20, -30, 50]
print(maxGameScore(cell))  # 40

Topic 2: Minimum Reversal

Problem Description Given a directed tree with n nodes (n-1 edges), select a root node such that all edges point outward from the root for the minimum number of edge reversals required, return this minimum number.

Example When \( n = 4 \), choosing node 2 as the root requires flipping edges 1→4, 2→4, and 3→4 a minimum of 2 times.

Approach to Solving the Problem Tree Dynamic Programming (DP) with Rerooting (or Reversing Root) Classic Problem

  • First DFS: Rooted at any node (node 1), compute the number of reversals.
  • Second DFS: When changing roots, only need to adjust the direction of parent-child edges, updating the total number of operations in O(1).
  • Ultimately, take the minimum value from all nodes in res.

Reference Code (Python)

Python

From collections import defaultdict

def minReversal(g_nodes, g_from, g_to):
    adj = defaultdict(list)
    for u, v in zip(g_from, g_to):
        adj[u].append((v, 0))
        adj[v].append((u, 1))

    res = [0] * (g_nodes + 1)
    visited = [False] * (g_nodes + 1)

    # First DFS: Calculate res[1]
    def dfs1(u):
        visited[u] = True
        total = 0
        for v, cost in adj[u]:
            if not visited[v]:
                total += cost + dfs1(v)
        return total

    res[1] = dfs1(1)
    visited = [False] * (g_nodes + 1)

    # Second DFS: Root Swapping
    def dfs2(u):
        visited[u] = True
        for v, cost in adj[u]:
            if not visited[v]:
                res[v] = res[u] + (1 if cost == 0 else -1)
                dfs2(v)

    dfs2(1)
    return min(res[1:g_nodes+1])

# Testing
g_nodes = 4
g_from = [1, 2, 3]
g_to = [4, 4, 4]
print(minReversal(g_nodes, g_from, g_to))  # 2

Uber OA Preparation Suggestions

  • Both of these questions are quite difficult; ordinary LeetCode medium difficulty problems aren't enough anymore.
  • Key Review: Jumping DP + Prime Preprocessing and Tree Root DP
  • Time is tight; it would be recommended to first write the core logic and then add boundaries and optimize.

Need more practice questions from the 2026 Uber OA exam, including the two in their Java and C++ versions. OA writing assistance / Voice Over (VO) real-time tutoring is available; feel free to message me at any time.

Programhelp offers:

  • 100% Pass-OA Writing (HackerRank / CodeSignal)
  • Voice-over Real-Time Thought Guidance
  • End-to-End Offer Assistance

Wish everyone good luck in snagging an Uber offer!

author avatar
Jory Wang Senior Software Engineer at Amazon
Senior Amazon Engineer with rich practical experience in the development of core systems for infrastructure, specializing in system scalability, reliability, and cost optimization. Currently focused on mentoring FAANG SDE interviews, helping over 30 candidates secure L5/L6 offers within a year.
END
 0