PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Meta

Implement exponentiation and fill grid distances

Last updated: Mar 29, 2026

Quick Overview

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.

  • medium
  • Meta
  • Coding & Algorithms
  • Machine Learning Engineer

Implement exponentiation and fill grid distances

Company: Meta

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

You are given two separate coding tasks. ## Task 1: Implement fast exponentiation Implement a function `pow(x, n)` that returns \(x^n\). - **Input**: - `x`: a real number (double/float) - `n`: a 32-bit signed integer (can be negative) - **Output**: the value \(x^n\) as a floating-point number - **Requirements / edge cases**: - Handle `n < 0` (e.g., \(x^{-n} = 1/x^n\)). - `n` may be `-2^31`, so be careful when negating. - Aim for \(O(\log |n|)\) time. ## Task 2: Fill shortest distance to a gate in a grid You are given an `m x n` grid of integers representing a map: - `-1` represents a **wall** (blocked cell) - `0` represents a **gate** - A large sentinel value (e.g., `INF = 2^31 - 1`) represents an **empty room** Update the grid **in-place** so that each empty room contains the distance (minimum number of moves) to its nearest gate. - Moves are allowed only in 4 directions: up/down/left/right. - If an empty room cannot reach any gate, it should remain `INF`. ### Example Input: ``` INF -1 0 INF INF INF INF -1 INF -1 INF -1 0 -1 INF INF ``` Output: ``` 3 -1 0 1 2 2 1 -1 1 -1 2 -1 0 -1 3 4 ``` - **Constraints (typical)**: - `1 <= m, n <= 200` (or similar) - Grid updates must be done without changing wall/gate cells.

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.

Related Interview Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)
Meta logo
Meta
Jan 8, 2026, 12:00 AM
Machine Learning Engineer
Take-home Project
Coding & Algorithms
3
0
Loading...

You are given two separate coding tasks.

Task 1: Implement fast exponentiation

Implement a function pow(x, n) that returns xnx^nxn.

  • Input :
    • x : a real number (double/float)
    • n : a 32-bit signed integer (can be negative)
  • Output : the value xnx^nxn as a floating-point number
  • Requirements / edge cases :
    • Handle n < 0 (e.g., x−n=1/xnx^{-n} = 1/x^nx−n=1/xn ).
    • n may be -2^31 , so be careful when negating.
    • Aim for O(log⁡∣n∣)O(\log |n|)O(log∣n∣) time.

Task 2: Fill shortest distance to a gate in a grid

You are given an m x n grid of integers representing a map:

  • -1 represents a wall (blocked cell)
  • 0 represents a gate
  • A large sentinel value (e.g., INF = 2^31 - 1 ) represents an empty room

Update the grid in-place so that each empty room contains the distance (minimum number of moves) to its nearest gate.

  • Moves are allowed only in 4 directions: up/down/left/right.
  • If an empty room cannot reach any gate, it should remain INF .

Example

Input:

INF  -1   0  INF
INF INF INF  -1
INF  -1 INF  -1
0    -1 INF INF

Output:

3   -1  0   1
2    2  1  -1
1   -1  2  -1
0   -1  3   4
  • Constraints (typical) :
    • 1 <= m, n <= 200 (or similar)
    • Grid updates must be done without changing wall/gate cells.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Meta•More Machine Learning Engineer•Meta Machine Learning Engineer•Meta Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.