Implement n-gram model and select n

Read the full interview experience this question came from →

Quick Overview

This interview question evaluates core ML concepts, assumptions, math intuition, training/evaluation trade-offs, and practical failure modes in a realistic interview setting. A strong answer for Implement n-gram model and select n states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Implement n-gram model and select n

Company: Runway

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Technical Screen

Implement an n-gram language model class with fit and generate methods. The fit(file_path, n) method should read a text file, tokenize consistently, build n-gram and (n− 1)-gram frequency counts, and compute conditional probabilities with smoothing (e.g., add-k or Kneser–Ney). The generate(start_tokens, max_len, sampling_strategy) method should sample next tokens according to learned probabilities (e.g., multinomial, top-k, or temperature) to produce text. Discuss how to select the optimal n given data size and domain: propose validation procedures (e.g., train/validation split), metrics (perplexity), regularization/backoff or interpolation, and analyze the time/space complexity and memory footprint for different n values.

Overview: This interview question evaluates core ML concepts, assumptions, math intuition, training/evaluation trade-offs, and practical failure modes in a realistic interview setting. A strong answer for Implement n-gram model and select n states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Read the full Runway Machine Learning Engineer interview experience this question came from

|Home/Machine Learning/Runway
Runway logo
Runway
Aug 8, 2025
hardMachine Learning EngineerTechnical ScreenMachine Learning
29
0

Implement n-gram model and select n

Task: Implement an n-gram Language Model with Training, Sampling, and Model Selection Guidance

Objective

Implement an n-gram language model class with the following methods and discuss model selection and complexity trade-offs:

  • fit(file_path, n):
    • Read and tokenize a text file consistently.
    • Build n-gram and (n−1)-gram frequency counts.
    • Compute conditional probabilities with smoothing (e.g., add-k or Kneser–Ney).
  • generate(start_tokens, max_len, sampling_strategy):
    • Sample next tokens according to learned probabilities (e.g., multinomial, top-k, temperature) to produce text.

Requirements and Notes

  • Tokenization must be consistent between training and generation. Include BOS/EOS handling if using sentence generation.
  • Smoothing options: implement at least add-k; explain and, if possible, implement interpolated Kneser–Ney.
  • Sampling strategies: support multinomial; add top-k and temperature scaling.
  • Model selection: discuss how to select the optimal n given data size and domain. Propose validation procedures (train/validation split), metrics (perplexity), regularization and backoff/interpolation.
  • Analyze time/space complexity and memory footprint for different n values.

Deliverables

  • Description of class design and data structures.
  • Clear pseudocode (or concise code sketch) for fit and generate.
  • Explanation of smoothing methods with formulas.
  • Explanation of sampling methods.
  • Strategy for choosing n with validation and perplexity.
  • Complexity and memory analysis.

Clarifying Questions to Ask Guidance

  • Clarify the task, data shape, labels, constraints, and evaluation metric.
  • State assumptions behind the math or modeling technique you choose.
  • Connect theory to practical training, debugging, and deployment implications.

What a Strong Answer Covers Guidance

  • Correct definitions and formulas where the prompt requires them.
  • A practical explanation of how the method behaves on real data.
  • Trade-offs, failure modes, diagnostics, and mitigation strategies.
  • Evaluation choices that match the product or modeling objective.

Follow-up Questions Guidance

  • How would noisy labels, class imbalance, or distribution shift affect the answer?
  • What would you monitor after deployment?
  • Which baseline would you compare against first?
Loading comments...