Practice
1. What is the time complexity of applying Min-Max scaling to a dataset with n samples and m features?
easy
Solution
Step 1: Understand Min-Max Scaling
Min-Max scaling requires computing the min and max for each feature, which involves iterating over all samples for each feature.Step 2: Calculate Complexity
For each of the m features, we iterate over n samples, resulting in O(n * m) complexity.Final Answer:
Option A -> Option AQuick Check:
Min-Max scaling is O(n * m). [OK]
Hint: Min-Max scaling is O(n * m). [OK]
Common Mistakes:
2.
import numpy as np
def compute_confusion_matrix(y_true, y_pred):
cm = np.zeros((2, 2), dtype=int)
for t, p in zip(y_true, y_pred):
cm[t][p] += 1
return cm
# Test case
y_true = [0, 1, 0, 1, 0, 1, 1]
y_pred = [0, 0, 0, 1, 1, 1, 1]
cm = compute_confusion_matrix(y_true, y_pred)
print(cm)Why might this implementation produce incorrect results?medium
Solution
Step 1: Analyze indexing logic
The code uses cm[t][p] which is incorrect for confusion matrix indexing.Step 2: Identify correct logic
Correct indexing should be cm[p][t] for predictions vs actuals.Final Answer:
Option A -> Option AQuick Check:
Confusion matrix should index [pred][true]. [OK]
Hint: Confusion matrix should index [pred][true]. [OK]
Common Mistakes:
3. How can you optimize the computation of cosine similarity for a large dataset of vectors?
hard
Solution
Step 1: Identify computational bottlenecks
Looping over pairs is inefficient for large datasets.Step 2: Optimize using matrix operations
Precomputing norms and using matrix multiplication reduces redundant calculations.Final Answer:
Option C -> Option CQuick Check:
Matrix ops reduce redundancy. [OK]
Hint: Matrix ops reduce redundancy. [OK]
Common Mistakes:
4. How can you optimize the implementation of linear regression using gradient descent for large datasets?
hard
Solution
Step 1: Identify scalability issues
Full-batch gradient descent is computationally expensive for large datasets.Step 2: Choose an optimization strategy
Mini-batch gradient descent reduces computation per iteration and can converge faster.Final Answer:
Option D -> Option DQuick Check:
Mini-batch reduces computation, speeds convergence. [OK]
Hint: Mini-batch reduces computation, speeds convergence. [OK]
Common Mistakes:
5. Which optimization would most effectively reduce the computational cost of the scaled dot-product attention mechanism for very large input sequences?
hard
Solution
Step 1: Identify the bottleneck
Computational cost is high due to dense matrix operations.Step 2: Choose the best optimization
Sparse matrices reduce computation by focusing on non-zero elements.Final Answer:
Option D -> Option DQuick Check:
Sparse matrices for large inputs [OK]
Hint: Sparse matrices for large inputs [OK]
Common Mistakes:
