Anomaly Detection — ML Technical Interview Questions & Answers
Spotting rare, unusual data points.
Anomaly Detection
- Simple meaning: Find the weird row — fraud, a broken sensor, a student whose hours/marks pair is impossible. Often unsupervised: Isolation Forest, one-class SVM, DBSCAN noise.
- Picture: 999 normal spends, 1 midnight ₹5 lakh in another country. Distance or isolation flags it. Then a human checks — flags are not proof.
- Imbalance is extreme. Accuracy is meaningless. Precision/recall on a labelled fraud set, or precision@k if you inspect the top 20 flags.
- Trap: deleting outliers before you know they are errors (they might be the point). Trap: one z-score on unscaled mixed columns.
Technical Interview Tip: Weird row ≠ always error. Flag, then inspect. Accuracy lies.
Code Examples for Anomaly Detection
Anomaly Detection — working example
# PCA-style intuition: project 2D → 1D by a direction vector
points = [(1.0, 1.0), (2.0, 2.0), (3.0, 2.8)]
# unit direction along y = x
ux, uy = 0.7071, 0.7071
proj = [x * ux + y * uy for x, y in points]
print([round(v, 3) for v in proj])
# Tiny ensemble: average two model votes
votes = [1, 0, 1]
v2 = [1, 1, 1]
ens = [1 if a + b >= 2 else 0 for a, b in zip(v1, v2)]
print(ens)Anomaly Detection — dim reduction compresses features; ensembles combine models; deep nets/RL/NLP are specialized families — practice the core math idea first.
How to Explain Anomaly Detection to an Interviewer
Unsupervised anomaly tools?
Isolation Forest DBSCAN noise One-class SVM
Why not accuracy?
Almost everything is ‘normal’