Implement snake game and find org LCA
Company: Atlassian
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Part A — Snake Game Engine: Implement a grid-based snake game. The board has height H and width W (0-indexed, row-major). The snake starts at (0,
0) moving right and initially occupies one cell. You are given a list of food positions as (row, col) that must be consumed in order. Expose an API: class Snake(int H, int W, List<(int,int)> food); int step(char dir) where dir ∈ {'U','D','L','R'}. On each step, the snake advances one cell; if the new head lands on the next food, increase score by 1 and grow (do not remove tail this turn); otherwise move the tail forward (no growth). Return the current score after a valid move, or -1 if the snake collides with walls or its own body (moving into the cell just vacated by the tail in the same step is allowed). Design data structures to support O(
1) time per step and implement the API. Describe time/space complexity.
Part B — Lowest Common Manager: Given an organization chart as a rooted n-ary tree with unique employee IDs and only child adjacency lists (no parent pointers), implement a function lowestCommonManager(root, a, b) that returns the lowest common manager of employees a and b. Handle cases where one or both employees may be absent. Discuss an approach optimized for a single query and an approach that supports many queries efficiently (e.g., preprocessing). Provide time/space complexity for both.
Quick Answer: This question evaluates proficiency in designing efficient data structures and APIs for real-time simulation (grid-based snake game) and in tree algorithms for computing lowest common ancestors, including handling edge cases, correctness, and formal time/space complexity analysis.
Snake Game Engine
Simulate a grid snake game and return the score after each move, or -1 after collision.
Constraints
- Board coordinates are 0-indexed
- Food must be eaten in list order
Examples
Input: (3, 3, [(0, 1), (0, 2)], ['R', 'R', 'D', 'L'])
Expected Output: [1, 2, 2, 2]
Explanation: Consumes food in order and grows.
Input: (2, 2, [], ['L', 'R'])
Expected Output: [-1, -1]
Explanation: Wall collision.
Input: (3, 3, [(0, 1), (1, 1)], ['R', 'D', 'L', 'U'])
Expected Output: [1, 2, 2, 2]
Explanation: Moving into vacated tail is allowed unless growing.
Hints
- Use a deque for the body and a set for occupied cells.
Lowest Common Manager
Given a rooted org tree as edges, return the lowest common manager of two employees or None if either is absent.
Constraints
- Employee IDs are unique in the tree
Examples
Input: ('CEO', [('CEO', 'VP1'), ('CEO', 'VP2'), ('VP1', 'A'), ('VP1', 'B')], 'A', 'B')
Expected Output: 'VP1'
Explanation: Employees under same VP.
Input: ('CEO', [('CEO', 'VP1'), ('CEO', 'VP2'), ('VP2', 'C')], 'VP1', 'C')
Expected Output: 'CEO'
Explanation: One queried id can be an internal node.
Input: ('CEO', [], 'A', 'CEO')
Expected Output: None
Explanation: Absent employee.
Hints
- Postorder DFS can return how many targets were found in each subtree.