xAI

Verified interview challenge

Implement K-nearest neighbours

Build a small classifier that predicts a query's label from its nearest training examples.

Unlock with Full Access

The challenge

Work directly with numeric training vectors and their labels rather than a ready-made classifier. Identify the nearest examples for a query and combine their votes under the specified tie rule. The challenge focuses on translating the algorithm into a correct implementation across valid input sizes and dimensions.

What you'll practise

  • Neighbour selection

    Compute distances and keep the selected points paired with their labels.

  • Deterministic prediction

    Handle the vote count and its boundary cases according to the contract.

Your coding workspacePreview
solution.pyPython 3.13
# Your implementation goes here

# Run examples and inspect results

# Full starter code included
Test cases

Run your code to see results.

Full instructions, starter code and test cases included with Full Access.