What distance metric would you use when features are cyclical, like angles or hours of the day?

Quick Overview

Asks which distance to use in nearest-neighbor models when features wrap around, such as angles or the hour of the day. Tests whether you can explain why plain differences fail, give a wrap-around distance or encoding, and scale it correctly next to other features.

What distance metric would you use when features are cyclical, like angles or hours of the day?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

You are building a k-nearest-neighbors model, and some of the features are cyclical: an angle, or the hour of the day. What distance metric would you use when features are cyclical like this? ```hint Look at the endpoints Compute the plain difference between hour 23 and hour 0, then ask how far apart those two times should be. ``` ### What a Strong Answer Covers - Why plain absolute differences fail on cyclical values - At least one distance or encoding that wraps around, with its formula - How each option fits standard nearest-neighbor tooling and its speed - How a cyclical feature is scaled and combined with the other features, and what to use for latitude and longitude ### Follow-up Questions - How would you handle day of the month, whose period changes from month to month? - With several cyclical and ordinary features, does Euclidean distance on the sin/cos columns still rank neighbors exactly as arc distance would? - Why must the sin and cos columns be scaled together and not separately? - Would you use the same encoding for a tree-based model?

Overview: Asks which distance to use in nearest-neighbor models when features wrap around, such as angles or the hour of the day. Tests whether you can explain why plain differences fail, give a wrap-around distance or encoding, and scale it correctly next to other features.

PracHub logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

You are building a k-nearest-neighbors model, and some of the features are cyclical: an angle, or the hour of the day. What distance metric would you use when features are cyclical like this?

What a Strong Answer Covers Guidance

  • Why plain absolute differences fail on cyclical values
  • At least one distance or encoding that wraps around, with its formula
  • How each option fits standard nearest-neighbor tooling and its speed
  • How a cyclical feature is scaled and combined with the other features, and what to use for latitude and longitude

Follow-up Questions Guidance

  • How would you handle day of the month, whose period changes from month to month?
  • With several cyclical and ordinary features, does Euclidean distance on the sin/cos columns still rank neighbors exactly as arc distance would?
  • Why must the sin and cos columns be scaled together and not separately?
  • Would you use the same encoding for a tree-based model?