Solve Tree Views, Columns, and Calculator
Company: Meta
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Onsite
The interview note referenced three separate coding tasks. Solve each task independently.
### Task 1: Binary tree side views
Given the root of a binary tree, return two arrays:
- `left_view[d]`: the value of the first node visible at depth `d` when looking at the tree from the left side
- `right_view[d]`: the value of the first node visible at depth `d` when looking at the tree from the right side
Example:
- Input tree:
- root = 1
- left child = 2, right child = 3
- node 2 has left child 4
- node 3 has right child 5
- Output: `[[1, 2, 4], [1, 3, 5]]`
### Task 2: Group a binary tree by vertical columns
Given the root of a binary tree, assign column `0` to the root. For any node at column `c`, its left child is at column `c - 1` and its right child is at column `c + 1`.
Return the node values grouped from the leftmost column to the rightmost column.
If multiple nodes fall in the same row and column, output them in left-to-right breadth-first order.
Example:
- Input tree:
- root = 3
- left child = 9, right child = 8
- children of 9 are 4 and 0
- children of 8 are 1 and 7
- Output: `[[4], [9], [3, 0, 1], [8], [7]]`
### Task 3: Evaluate a basic arithmetic string
Given a string expression containing non-negative integers, spaces, and the operators `+`, `-`, `*`, and `/`, return the evaluated integer result.
- Division truncates toward zero.
- The expression contains no parentheses.
Example:
- Input: `"3+2*2"`
- Output: `7`
Reasonable constraints for all tasks:
- Up to `10^5` nodes for tree problems
- Expression length up to `10^5`
Quick Answer: This multi-part question evaluates skills in binary tree traversal and view extraction, vertical column grouping and ordering of tree nodes, and parsing and evaluation of arithmetic expressions with operator precedence and integer division semantics.