Implement exponentiation and fill grid distances
Company: Meta
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question evaluates algorithmic problem-solving skills in the Coding & Algorithms domain, specifically numeric algorithms for fast exponentiation and graph traversal techniques for grid-based shortest-path computation (multi-source BFS) with attention to edge-case handling.
Fast Exponentiation
Constraints
- n is a 32-bit signed integer
- Handle negative n without overflowing when negating in fixed-width languages
Examples
Input: (2.0, 10)
Expected Output: 1024.0
Input: (2.0, -2)
Expected Output: 0.25
Input: (2.0, 0)
Expected Output: 1.0
Input: (0.0, -1)
Expected Output: None
Input: (-2.0, 3)
Expected Output: -8.0
Hints
- Square the base while halving the exponent.
Fill Distance to Nearest Gate
Constraints
- Movement is 4-directional
- Do not change walls or gates
Examples
Input: ([[2147483647, -1, 0, 2147483647], [2147483647, 2147483647, 2147483647, -1], [2147483647, -1, 2147483647, -1], [0, -1, 2147483647, 2147483647]],)
Expected Output: [[3, -1, 0, 1], [2, 2, 1, -1], [1, -1, 2, -1], [0, -1, 3, 4]]
Input: ([[2147483647, -1], [-1, 0]],)
Expected Output: [[2147483647, -1], [-1, 0]]
Input: ([[2147483647]],)
Expected Output: [[2147483647]]
Hints
- Start BFS from all gates at once.