Implement power, reduce string, parse tree, debug maze
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
## 1) Implement fast exponentiation (`pow`)
Implement a function `pow(x, n)` that returns \(x^n\).
- Inputs: a floating-point base `x` and an integer exponent `n` (can be negative).
- Output: `x` raised to the power `n`.
- Requirements:
- Improve time efficiency beyond multiplying `x` `|n|` times.
- Handle edge cases such as `n = 0`, negative `n`, and large `|n|`.
## 2) Cancel/reduce a string by repeatedly removing adjacent duplicates
Given a string `s`, repeatedly remove any **maximal contiguous group** of the same character with length \(\ge 2\). Continue until no more removals are possible. Return the final string.
Examples:
- `"abbba"` → `""`
(remove `bbb` → `"aa"`, then remove `aa` → `""`)
- `"Acbcccbac"` → `"Acac"`
(one possible sequence: remove `ccc` → `"Acbbac"`, remove `bb` → `"Acac"`)
Clarify in your solution whether the operation is case-sensitive (assume case-sensitive unless stated otherwise).
## 3) Build a binary tree from a parenthesized string
You are given a string encoding of a binary tree using this grammar:
- A node is encoded as: `value(leftSubtree)(rightSubtree)`
- `value` is an integer (may be multi-digit; may be negative).
- Each subtree is encoded the same way.
- A missing child may be represented by empty parentheses: `()`.
Task: Parse the string and construct the corresponding binary tree, returning the root.
Example (illustrative):
- Input: `"5(2() (3()()))(1()())"` (whitespace optional)
- Output: a tree with root value `5`, left child rooted at `2`, and right child rooted at `1`.
## 4) Maze pathfinding: find and fix bugs, then extend features
You are given buggy code for solving a maze/grid pathfinding problem (e.g., DFS/BFS from a start to an end cell). The interviewer asks you to:
1. **Fix a correctness bug**: the code prints an incorrect path because it fails to check whether the “current” cell is actually empty/passable before processing it.
2. **Fix a performance bug**: the maze solver is too slow or may revisit states; add a `visited` set / hash set to prevent revisiting cells.
3. **Extend the maze with one-way doors**: add a feature where certain cells represent doors that only allow traversal in one direction (e.g., you may pass through the door only when moving left→right, but not right→left). Ensure the solver respects one-way constraints.
- Discuss corner cases such as a door adjacent to a wall where naive logic could incorrectly block valid paths.
Define your movement rules (4-neighbor vs 8-neighbor), cell encodings (walls, empty, start/end, doors), and what output is required (existence, any path, or shortest path).
Quick Answer: This multi-part question evaluates numerical algorithms (fast exponentiation), string-processing and reduction logic, recursive parsing and tree construction, and graph search with debugging and constraint handling, covering algorithmic efficiency, parsing accuracy, state management, and edge-case reasoning.
Fast Power
Return x raised to integer exponent n using exponentiation by squaring.
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
Examples
Input: (2.0, 10)
Expected Output: 1024.0
Explanation: Fast exponentiation squares the base.
Input: (2.0, -2)
Expected Output: 0.25
Explanation: Negative exponents return reciprocals.
Input: (5.0, 0)
Expected Output: 1.0
Explanation: Any nonzero base to exponent zero is one.
Hints
- Clarify edge cases before coding.
- Keep the return value deterministic.
Reduce Adjacent Duplicate Groups
Repeatedly remove maximal contiguous groups of the same character with length at least two and return the final string.
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
Examples
Input: ('abbba',)
Expected Output: 'aba'
Explanation: Removing bbb exposes aa, which is then removed.
Input: ('Acbcccbac',)
Expected Output: 'Acbcbac'
Explanation: The process is case-sensitive.
Input: ('abc',)
Expected Output: 'abc'
Explanation: No adjacent group of length at least two remains unchanged.
Hints
- Clarify edge cases before coding.
- Keep the return value deterministic.
Parse Parenthesized Binary Tree
Parse value(left)(right) binary-tree notation and return a nested [value,left,right] representation with None for missing children.
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
Examples
Input: ('5(2()(3()()))(1()())',)
Expected Output: [5, [2, None, [3, None, None]], [1, None, None]]
Explanation: Parse a tree with missing and present children.
Input: ('-10()()',)
Expected Output: [-10, None, None]
Explanation: Negative multi-digit values are supported.
Input: ('',)
Expected Output: None
Explanation: Empty input returns None.
Hints
- Clarify edge cases before coding.
- Keep the return value deterministic.
Maze Shortest Path With One-Way Doors
Return the shortest 4-neighbor path length from start to end in a grid with walls and directional door cells, or -1 if unreachable.
Constraints
- # is a wall; other non-arrow cells are passable.
- Arrow cells > < ^ v may only be entered while moving in that arrow direction.
Examples
Input: (["S..","##.","..E"], (0,0), (2,2))
Expected Output: 4
Explanation: BFS finds the shortest valid path.
Input: (["S#E"], (0,0), (0,2))
Expected Output: -1
Explanation: Walls block the path.
Input: (["S>E"], (0,0), (0,2))
Expected Output: 2
Explanation: A right-facing door can be entered from left to right.
Input: (["E<S"], (0,2), (0,0))
Expected Output: 2
Explanation: A left-facing door can be entered from right to left.
Hints
- Clarify edge cases before coding.
- Keep the return value deterministic.