Master Interview Like a Ninja

AI Interview Copilot and Assistant

Preparing for a Facebook (MetaNow) interview can feel like a daunting challenge, but don’t worry—I’ve got you covered! In this post, I’ll walk you through 10 of the most common interview questions asked at Facebook, complete with clear explanations, examples, and step-by-step solutions. By the end, you’ll have a much better grasp of how to tackle these problems confidently. Plus, I’ll share how I’ve personally leveraged Ninjafy AI as my interview copilot to ace mock interviews and refine my skills.


Table of Contents

  1. Merge Intervals
  2. LRU Cache
  3. Subarray Sum Equals K
  4. K Closest Points to the Origin
  5. Basic Calculator
  6. Vertical Order Traversal of a Binary Tree
  7. Implement Power Function
  8. Random Pick with Weight
  9. Range Sum of BST
  10. Valid Palindrome

图片描述


1. Merge Intervals

Problem:
You are given a list of intervals where each interval is a tuple, e.g., (1, 3), representing the start and end points. Your task is to merge overlapping intervals.

Example:
Input: [(1, 3), (2, 6), (8, 10), (15, 18)]
Output: [(1, 6), (8, 10), (15, 18)]

Solution:

  1. Sort intervals by their start time.
  2. Iterate through intervals:
    • If the current interval overlaps with the previous one, merge them by updating the end time.
    • If no overlap, add the interval to the result list.
def merge_intervals(intervals):
    intervals.sort(key=lambda x: x[0])
    result = [intervals[0]]

    for start, end in intervals[1:]:
        last_end = result[-1][1]
        if start <= last_end:
            result[-1][1] = max(last_end, end)
        else:
            result.append([start, end])
    return result

Why it works:
Sorting ensures intervals are processed sequentially, and merging occurs only when intervals overlap.


2. LRU Cache

Problem:
Design a Least Recently Used (LRU) Cache with get(key) and put(key, value) operations. The cache should have a fixed capacity and evict the least recently used items when full.

Example:
Input:

cache = LRUCache(2)
cache.put(1, 1)
cache.put(2, 2)
cache.get(1)  # returns 1
cache.put(3, 3)  # evicts key 2
cache.get(2)  # returns -1 (not found)
cache.put(4, 4)  # evicts key 1
cache.get(1)  # returns -1
cache.get(3)  # returns 3
cache.get(4)  # returns 4

Solution:
Use an OrderedDict from Python’s collections module. It maintains insertion order, which simplifies the LRU eviction.

from collections import OrderedDict

class LRUCache:
    def __init__(self, capacity):
        self.cache = OrderedDict()
        self.capacity = capacity

    def get(self, key):
        if key in self.cache:
            self.cache.move_to_end(key)  # Recently used
            return self.cache[key]
        return -1

    def put(self, key, value):
        if key in self.cache:
            self.cache.move_to_end(key)
        self.cache[key] = value
        if len(self.cache) > self.capacity:
            self.cache.popitem(last=False)  # Remove least recently used

3. Subarray Sum Equals K

Problem:
Find the number of subarrays in an array that sum to a given value k.

Example:
Input: nums = [1, 1, 1], k = 2
Output: 2
Explanation: [1, 1] appears twice.

Solution:
Use a hash map to store the cumulative sum and track how often a specific sum has occurred.

def subarray_sum(nums, k):
    count = 0
    current_sum = 0
    prefix_sums = {0: 1}

    for num in nums:
        current_sum += num
        count += prefix_sums.get(current_sum - k, 0)
        prefix_sums[current_sum] = prefix_sums.get(current_sum, 0) + 1

    return count

Visualization Table:

Index Num Cumulative Sum Current Sum – K Count Prefix Sums
0 1 1 -1 0 {0: 1, 1: 1}
1 1 2 0 1 {0: 1, 1: 1, 2: 1}
2 1 3 1 2 {0: 1, 1: 1, 2: 1, 3: 1}

图片描述


4. K Closest Points to the Origin

Problem:
Given points on a 2D plane and an integer k, find the k closest points to the origin (0, 0).

Example:
Input: points = [[3, 3], [5, -1], [-2, 4]], k = 2
Output: [[3, 3], [-2, 4]]

Solution:

  1. Calculate the squared distance for each point.
  2. Use a min-heap to store the closest k points.
import heapq

def k_closest(points, k):
    return heapq.nsmallest(k, points, key=lambda x: x[0]**2 + x[1]**2)

Leveraging Ninjafy AI for Success

Over the course of preparing for these challenging interview questions, I’ve relied on Ninjafy AI as my interview copilot. It has been especially helpful during mock interviews, where I practiced problem-solving under time constraints. The platform’s NinjaCopilot™ provided tailored guidance for live interviews, and its InvisibleEyetrack™ technology ensured I maintained confident eye contact throughout. For anyone navigating the tricky landscape of situational and behavioral questions, this assistant is a game-changer!


Stay tuned for Part 2, where we’ll dive into the remaining questions, including Random Pick with Weights, Range Sum of BST, and more!