Skip to content

Machine Learning & AI Interview Questions

ML concepts, algorithms, deep learning, LLMs, and AI application design.


1. What's the difference between supervised and unsupervised learning?

View Answer

Supervised Learning: Labeled Data

Learn from labeled examples (input → output pairs).

Regression: Predict Continuous Values

Example: Predict house price from features

Input: [size, beds, location] → Output: $500,000
Input: [size, beds, location] → Output: $750,000
Model learns: price = 100*size + 50*beds + ...

Classification: Predict Categories

Example: Spam detector

Input: Email text → Output: [Spam, Not Spam]
Input: Medical scan → Output: [Cancer, Benign]

Unsupervised Learning: No Labels

Find patterns in unlabeled data.

Clustering: Group Similar Items

Input: Customer purchase histories
No labels
Model discovers: 3 customer segments

Dimensionality Reduction: Simplify Data

Input: 1000 features
Output: 50 most important features
Reduces noise, improves efficiency

Comparison Table

Property Supervised Unsupervised
Data Labeled (x, y) Unlabeled (x only)
Goal Predict labels Find patterns
Evaluation Easy (compare to labels) Hard (no ground truth)
Effort Labeling is expensive No labeling needed
Examples Classification, Regression Clustering, Dimension Reduction

📚 Learn more: ML Fundamentals


2. Explain overfitting and how to prevent it

View Answer

Overfitting: Memorizing Data

Model learns training data too well, including noise. Fails on new data.

Training data: High accuracy (99%)
Test data: Low accuracy (60%)
→ Overfitting: Model memorized training, doesn't generalize

Example

Training: Learning exact student names for exam answers
→ Works on training set (100%)
→ Fails on new students (0%)
vs
Learning the underlying concepts
→ Works on training (85%)
→ Works on new data (82%)

How to Detect Overfitting

Plot: Training vs Test Accuracy
         Training ─────────────
         Test        ──────
         Gap = overfitting

Prevention Strategies

Technique How It Works When to Use
Regularization Penalize complex models (L1/L2) Always (linear, neural nets)
Dropout Randomly disable neurons during training Deep neural networks
Early Stopping Stop training when validation error increases Neural networks
Cross-Validation Train/test on multiple data splits All algorithms
More Data Larger training set reduces memorization Always helps
Simpler Model Fewer parameters, less capacity When possible

Regularization Example

Loss = Error + λ * (model complexity)
       ↑         ↑
   accuracy    penalty

If λ high: Model stays simple
If λ low: Can overfit

📚 Learn more: Model Validation


3. What's the bias-variance tradeoff?

View Answer

Bias: Underfitting

Model too simple, misses true patterns.

Example: Fitting straight line to curved data
Result: Poor fit on both training and test
Prediction: Consistently wrong

Variance: Overfitting

Model too complex, learns noise.

Example: Fitting polynomial to noisy data
Result: Perfect fit on training, poor on test
Prediction: High variability with small data changes

The Tradeoff

Model Complexity →
Bias:   High → Low → Low
Variance: Low → Low → High
             Goldilocks zone

Total Error = Bias² + Variance + Irreducible Error

Scenario Bias Variance Problem Solution
Simple Model High Low Underfitting More complex
Complex Model Low High Overfitting Regularize
Just Right Medium Medium Optimal Good model

Visual Representation

Underfitting          Optimal            Overfitting
○ ○ ○ ○              ○ ╲               ╲ ○ ╱
○ ○ ● ○              ○ ╲ ●             ╲ ● ╱
○ ● ○ ○              ● ╲ ○             ╱ ○ ╲
(straight line)   (slight curve)    (wiggly line)

📚 Learn more: Bias-Variance


4. Explain how CNNs work and when to use them

View Answer

CNN: Convolutional Neural Network

Specialized neural network for image/spatial data.

Key Concept: Convolution

Filter (3x3):        Image:
[1 0 -1]            [5 2 8]
[2 0 -2]     *      [3 1 4]
[1 0 -1]            [7 9 2]
       ↓
Applies filter across image, detecting features

CNN Layers

Layer Purpose Output
Input Image (e.g., 28×28×3) Pixels
Convolutional Detect features (edges, shapes) Feature maps
ReLU Activation (non-linearity) Sparse activation
Pooling Reduce dimensionality Smaller feature maps
Flatten Convert to 1D vector Feature vector
Dense Classification Probabilities

Why CNNs for Images?

  • ✅ Spatial locality: Nearby pixels are related
  • ✅ Parameter sharing: Same filter detects feature anywhere
  • ✅ Hierarchical: Simple features → complex features
  • ✅ Translation invariant: Detects object anywhere in image

When to Use CNNs

  • ✅ Image classification: Cat/dog detector
  • ✅ Object detection: Find objects in images
  • ✅ Segmentation: Pixel-level classification
  • ✅ Face recognition: Identify faces
  • ❌ Not for: Tabular data, time series (use RNN/LSTM)

📚 Learn more: Deep Learning & CNNs


5. Explain the transformer architecture and attention mechanism

View Answer

Transformer: Revolution in NLP

Architecture based on attention, enables parallel processing.

Attention Mechanism: Focus on Relevant Info

Model learns which parts of input are important for each output.

Sentence: "The bank is on the river"
Word: "bank"
Attention: Focus on "river" (context clue)
           Less focus on "the"

Multi-Head Attention

Multiple attention heads looking at different aspects:
  Head 1: Subject-verb relationships
  Head 2: Adjective-noun relationships
  Head 3: Long-range dependencies
→ Combine all heads → Rich representation

Transformer Architecture

Input: "Hello world"
  ↓
Embedding (word vectors)
  ↓
Positional Encoding (position info)
  ↓
Encoder Stack:
  - Multi-head Attention
  - Feed-forward Network
  (repeated 6-12 times)
  ↓
Decoder Stack:
  - Multi-head Attention
  - Cross-attention to Encoder
  - Feed-forward Network
  ↓
Output: "Hola mundo"

Why Transformers are Powerful

  • ✅ Parallel: Process all words simultaneously (vs RNN sequential)
  • ✅ Long-range: Attention captures distant dependencies
  • ✅ Scalable: Train on massive datasets
  • ✅ Transfer learning: Pre-train, fine-tune for tasks

Transformer Variants

Model Type Use Case
BERT Encoder-only Understanding (classification, Q&A)
GPT Decoder-only Generation (text completion, chat)
T5 Encoder-Decoder Seq2seq (translation, summarization)

📚 Learn more: Transformers & LLMs


6. What's RAG and why is it useful?

View Answer

RAG: Retrieval-Augmented Generation

Combine information retrieval with language generation to ground LLM responses in facts.

Problem: LLM Hallucinations

LLMs generate plausible-sounding but false information.

User: "What's the release date of Avatar 3?"
LLM (without RAG): "Avatar 3 was released on December 15, 2024"
   ❌ Might be wrong, outdated, or made up

RAG Solution

1. Retrieve relevant documents from knowledge base
2. Pass documents + query to LLM
3. LLM generates answer grounded in retrieved facts

RAG Workflow

User Query: "What's Avatar 3 release date?"
  ↓
Retriever: Search knowledge base
  ↓ (finds relevant documents)
Relevant docs: "Avatar: The Way of Water released Dec 16, 2022"
  ↓
LLM: Generate answer using retrieved docs
  ↓
Output: Grounded response with sources

Components

Component Purpose Technology
Vector Embeddings Convert text to vectors OpenAI embeddings, Hugging Face
Vector DB Store/search embeddings Pinecone, Weaviate, Milvus
Retriever Find relevant documents Semantic search
LLM Generate answer GPT-4, Llama, Claude

Benefits

  • ✅ Factual accuracy: Grounds in retrieved documents
  • ✅ Updated knowledge: Can index new documents
  • ✅ Sources: Can cite where answer came from
  • ✅ Cost: Smaller LLM sufficient (don't need largest models)

Example: Document QA

Upload: Company employee handbook
User: "What's our leave policy?"
RAG: Retrieves relevant section
     Generates answer from policy
     Cites policy document

📚 Learn more: RAG & Generative AI


7. What's the difference between precision and recall?

View Answer

Classification Confusion Matrix

Predicted Positive Predicted Negative
Actual Positive TP (True Positive) FN (False Negative)
Actual Negative FP (False Positive) TN (True Negative)

Precision: False Positive Rate

Of positive predictions, how many are correct?

Precision = TP / (TP + FP)

Email spam detector:
  "How many emails we flagged as spam were actually spam?"
  High precision = fewer false spam alerts (better UX)

Recall: False Negative Rate

Of actual positives, how many did we find?

Recall = TP / (TP + FN)

Email spam detector:
  "Of all spam emails, how many did we catch?"
  High recall = fewer spam in inbox (better filtering)

Tradeoff: Precision vs Recall

Strict model (high threshold)
→ High precision, low recall
→ Few false positives, many false negatives

Lenient model (low threshold)
→ Low precision, high recall
→ Many false positives, few false negatives

When to Optimize

Scenario Optimize For Why
Medical diagnosis Recall Don't miss patients with disease
Spam detection Precision Don't block legitimate emails
Document search Precision Return relevant results
Fraud detection Recall Catch all fraud

F1 Score: Balance Both

F1 = 2 × (Precision × Recall) / (Precision + Recall)
     ↑
   Harmonic mean, penalizes imbalance

📚 Learn more: Model Evaluation


8. How do you detect and handle model drift?

View Answer

Model Drift: Performance Degradation

Model performance decreases over time on new data.

Deployment day: Accuracy 95%
Month 1: Accuracy 94%
Month 3: Accuracy 88%
Year 1: Accuracy 72% ← Model drift!

Types of Drift

Data Drift (Covariate Shift)

Input data distribution changed.

Example: Fraud detection
  Training: 2020 transaction patterns
  2024: Different payment methods, new fraud tactics
  Model sees unexpected patterns

Concept Drift (Label Drift)

Relationship between input and output changed.

Example: Stock prediction
  2020: Certain features predicted price well
  2024: Market dynamics changed, relationships different

Detection Methods

Method How It Works Example
Performance monitoring Track accuracy/metrics over time Alert if accuracy drops 5%
Statistical tests Compare data distributions (Kolmogorov-Smirnov) Detect if data shifted significantly
Prediction confidence Monitor prediction uncertainty If confidence drops, data might have drifted
Ground truth comparison Compare predictions to actual labels Manual labeling on sample, measure error

Handling Drift

  • 1. Retrain: Periodically retrain on new data
  • 2. Continuous learning: Update model incrementally
  • 3. Ensemble: Multiple models for robustness
  • 4. Monitoring: Set up alerts for performance drop
  • 5. Feature engineering: Add new relevant features

Retraining Strategy

Weekly:
  - Collect new labeled data
  - Measure performance
  - If accuracy drops > 2%: Retrain
  - A/B test new model
  - Deploy if better

Example: Email Spam Detector

Month 0: 95% accuracy
Month 3: New spam tactics emerge, 90% accuracy
Action: Retrain on recent emails with new patterns
Result: 94% accuracy restored

📚 Learn more: Model Monitoring


See also: ML Fundamentals, Algorithms, Deep Learning, Transformers & LLMs, RAG & Generative AI, Interview Questions Overview