Solve two interview coding problems
UberSoftware Engineer · Technical ScreenLast 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, wherenodes[i]is the character at nodeiint[] nodeFromandint[] nodeTo, where each pair describes an edge in the treeint[] 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 = 4nodes = ['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"-> valid3 -> 1, characters"aa"-> valid3 -> 1 -> 0, characters"aaz"-> can be rearranged into"aza"-> valid
So the result is [3].
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.