Katkat

Solve two interview coding problems

UberSoftware Engineer · Technical Screen

Last updated

These two problems evaluate algorithmic problem-solving skills: the first focuses on spatial reasoning and grid-based distance computation relative to obstacles, while the second targets tree traversal and character-frequency reasoning for ancestor-path palindrome checks.

The interview included two coding problems.

Problem 1: Find valid robot locations in a grid

You are given a 2D grid containing empty cells and obstacles. A robot may be placed on any empty cell. For a candidate robot position, define a 4-element signature [left, up, right, down], where each value is the number of steps from the robot's cell to the nearest obstacle in that direction. Only obstacles in the same row or column count. If there is no obstacle before the grid boundary in any direction, that candidate does not match.

Given the grid and a target signature such as [1, 1, 2, 2], return all robot locations (row, col) whose directional distances exactly match the target.

Problem 2: Count ancestor path segments that can form a palindrome

You are given a rooted tree with treeNodes nodes labeled 0..treeNodes-1, where node 0 is the root. Each node stores a lowercase letter:

  • char[] nodes, where nodes[i] is the character at node i
  • int[] nodeFrom and int[] nodeTo, where each pair describes an edge in the tree
  • int[] queries, where each query is a start node

For a query node q, consider every path segment that starts at q and ends at an ancestor of q, including q itself and the root. For each such segment, collect the characters along the path. Count how many of those path segments can be rearranged into a palindrome.

A string can be rearranged into a palindrome if at most one character has an odd frequency.

Return one answer per query.

Example:

  • treeNodes = 4
  • nodes = ['z', 'a', 'a', 'a']
  • nodeFrom = [0, 0, 1]
  • nodeTo = [1, 2, 3]
  • queries = [3]

For query 3, the ancestor path segments are:

  • 3 -> 3, characters "a" -> valid
  • 3 -> 1, characters "aa" -> valid
  • 3 -> 1 -> 0, characters "aaz" -> can be rearranged into "aza" -> valid

So the result is [3].