What are the training and prediction time complexities of brute-force KNN with N samples and D features?

Quick Overview

Asks for the training and per-query time complexity of brute-force k-nearest neighbors with N samples and D features. It tests whether you can count the distance and selection costs, reason about query batches and memory, and know when tree or approximate indexes help.

What are the training and prediction time complexities of brute-force KNN with N samples and D features?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

What are the training and prediction time complexities of brute-force k-nearest neighbors (KNN) with $N$ training samples and $D$ features? ```hint Split the work Count separately what happens when the model is fit and what happens for each query, including the step that picks the k closest points. ``` ### What a Strong Answer Covers - Training time and memory for a lazy learner - The per-query distance cost in terms of $N$ and $D$ - The cost of selecting the k nearest points with a full sort, a heap or partial selection - The cost for a batch of $M$ queries and how it is computed in practice - How tree indexes and approximate nearest neighbor indexes change query time, and where each applies ### Follow-up Questions - Why do KD-trees stop helping as $D$ grows? - How would you serve nearest neighbor queries over 100 million vectors with a 10 ms latency budget? - The squared-distance expansion sometimes returns a small negative number. Why, and how do you handle it? - How does the choice of distance metric affect the cost and the indexes you can use?

Overview: Asks for the training and per-query time complexity of brute-force k-nearest neighbors with N samples and D features. It tests whether you can count the distance and selection costs, reason about query batches and memory, and know when tree or approximate indexes help.

General logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

What are the training and prediction time complexities of brute-force k-nearest neighbors (KNN) with NN training samples and DD features?

What a Strong Answer Covers Guidance

  • Training time and memory for a lazy learner
  • The per-query distance cost in terms of NN and DD
  • The cost of selecting the k nearest points with a full sort, a heap or partial selection
  • The cost for a batch of MM queries and how it is computed in practice
  • How tree indexes and approximate nearest neighbor indexes change query time, and where each applies

Follow-up Questions Guidance

  • Why do KD-trees stop helping as DD grows?
  • How would you serve nearest neighbor queries over 100 million vectors with a 10 ms latency budget?
  • The squared-distance expansion sometimes returns a small negative number. Why, and how do you handle it?
  • How does the choice of distance metric affect the cost and the indexes you can use?