← Meta Interview Insights

Meta·Software Engineer·Onsite - Coding / Algorithms·Senior

SeniorPrefer not to say
May 2026

Summary

Meta SWE interview with three back-to-back coding tasks that mixed classic algorithms with from-scratch ML implementation. No ML libraries allowed, which I was not expecting going in.

Questions Asked (3)

Q1

Given a string, find the length of the longest contiguous substring where all characters are distinct.

Algorithms & Data Structures
Author's notes

Classic sliding window.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a sliding window with a hash map to track the last seen index of each character, expanding the right pointer and shrinking the left pointer when a duplicate is found. This yields an O(n) time and O(min(n, alphabet)) space solution. Clearly explain the invariant that the window always contains distinct characters.

Pro tip: After presenting the optimal solution, mention the brute-force O(n^2) approach and why it's inefficient, then discuss edge cases like empty string, all unique characters, and all same characters. This shows you think about trade-offs and robustness.

1. Clarify the problem

Confirm that the substring must be contiguous and characters are case-sensitive. Ask about input constraints (e.g., ASCII vs Unicode) to decide on data structures.

2. Discuss brute force

Briefly outline the O(n^2) approach of checking all substrings for uniqueness, and explain why it's suboptimal for large inputs.

3. Propose sliding window

Introduce the sliding window technique with two pointers (left and right) and a hash map to store the last index of each character. Explain how the window expands and contracts.

4. Walk through an example

Trace the algorithm on a sample string like 'abcabcbb' to demonstrate how the window updates and the maximum length is tracked.

5. Analyze complexity and edge cases

State O(n) time and O(min(n, alphabet)) space. Mention edge cases: empty string, single character, all unique, all duplicates, and strings with spaces or special characters.

Key Points to Mention

  • Sliding window technique with two pointers
  • Hash map to store last seen index of each character
  • Time complexity O(n) and space complexity O(min(n, alphabet))
  • Handling duplicates by moving left pointer to max(left, last_seen[char] + 1)
  • Edge cases: empty string, all unique, all same characters
  • Comparison with brute force O(n^2) approach

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Implement binary logistic regression from scratch, including fit, predict_proba, and predict methods, using only batch gradient descent and no ML libraries.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where things got spicy.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem scope and assumptions (e.g., binary labels, feature matrix shape, no regularization). Then outline the logistic regression model, loss function, and gradient derivation, and finally walk through the implementation of fit (batch gradient descent), predict_proba (sigmoid), and predict (thresholding).

Pro tip: Mention numerical stability techniques like clipping the sigmoid output or using the log-sum-exp trick, and discuss convergence criteria (e.g., gradient norm tolerance) to show production-level awareness.

1. Clarify requirements and assumptions

Confirm input/output formats, binary classification, and that only batch gradient descent is allowed. Discuss initialization (zeros) and stopping criteria (max iterations or gradient tolerance).

2. Derive the model and gradient

Write the sigmoid function and log-likelihood loss. Derive the gradient of the loss with respect to weights and bias, showing that it simplifies to X^T (sigmoid(Xw+b) - y) / m.

3. Implement fit with batch gradient descent

Initialize weights and bias, then loop for a fixed number of iterations or until convergence. In each iteration, compute predictions, gradient, and update parameters using learning rate.

4. Implement predict_proba and predict

predict_proba returns the sigmoid of the linear combination. predict applies a threshold (e.g., 0.5) to the probabilities to output binary labels.

5. Discuss trade-offs and optimizations

Mention vectorization for efficiency, handling large datasets, and potential improvements like stochastic gradient descent or regularization, while noting the constraint of batch GD.

Key Points to Mention

  • Sigmoid function and its role in logistic regression
  • Binary cross-entropy loss and its gradient derivation
  • Batch gradient descent update rule: w = w - learning_rate * gradient
  • Vectorized implementation using NumPy for efficiency
  • Numerical stability: clipping sigmoid outputs to avoid log(0)
  • Convergence criteria: max iterations, gradient norm tolerance, or loss change threshold

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

Implement a multinomial Naive Bayes text classifier from scratch, including Laplace smoothing, log-space computation, and handling unknown tokens at test time.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Probably the hardest of the three for me.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem scope and assumptions, then outline the training and prediction phases. Emphasize the need for Laplace smoothing to handle unseen words, log-space computation to avoid underflow, and a strategy for unknown tokens at test time. Walk through the algorithm step-by-step, discussing trade-offs and edge cases.

Pro tip: Mention that you would use a defaultdict for counts and apply Laplace smoothing with alpha=1 by default, but be prepared to discuss how to tune alpha. Also, highlight that handling unknown tokens can be done by either ignoring them or mapping to a special <UNK> token if the training data included it.

1. Clarify requirements and assumptions

Ask about the dataset size, number of classes, and whether tokenization is provided. Confirm that Laplace smoothing is required and that unknown tokens should be handled gracefully.

2. Design the training phase

Compute class priors and conditional probabilities for each word given each class. Use Laplace smoothing to avoid zero probabilities, and store log probabilities to prevent underflow.

3. Implement prediction in log-space

For a test document, sum the log priors and log likelihoods of its tokens for each class, then choose the class with the highest score. Handle unknown tokens by skipping them or using a predefined <UNK> probability.

4. Address edge cases and optimizations

Discuss how to handle empty documents, tokens not seen in training, and potential numerical stability issues. Mention that log-sum-exp can be used if probabilities are needed.

5. Analyze trade-offs and complexity

Compare multinomial vs. Bernoulli Naive Bayes, discuss time and space complexity, and note that smoothing parameter alpha can be tuned via cross-validation.

Key Points to Mention

  • Laplace smoothing formula: (count(word, class) + alpha) / (total words in class + alpha * vocabulary size)
  • Log-space computation to avoid underflow: sum of log probabilities instead of product
  • Handling unknown tokens: either ignore them or use a special <UNK> token with a smoothed probability
  • Class priors: log P(class) = log(count(class) / total documents)
  • Prediction: argmax over classes of log P(class) + sum(log P(word|class))
  • Trade-offs: multinomial assumes word independence and captures frequency, while Bernoulli captures presence/absence

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.