
ML Engineer Interview Question from Quora & Other Companies
Whether you’re preparing for your next ML Engineer interview or brushing up on key concepts, these detailed solutions and explanations will help you strengthen your foundational knowledge.
ML Engineer Interview Question from Quora & Other Companies
1. Serialize and Deserialize BST (ML Engineer at Quora)
Understanding the Problem
Serialization is the process of converting a data structure into a string so that it can be stored or transmitted. Deserialization is the reverse process — reconstructing the original structure from the string. For a Binary Search Tree (BST), serialization and deserialization should preserve the BST’s properties.
Key Concepts
- BST Properties: For any node, its left subtree contains only nodes with values less than the node’s value, and its right subtree contains only nodes with values greater than the node’s value.
- Traversal: Preorder traversal (root, left, right) is often used for serialization, as it preserves the root-first order.
Solution Approach
- Serialize: Use preorder traversal to convert the BST into a string.
- Deserialize: Reconstruct the BST from the preorder string by recursively defining value range constraints.
Python Implementation
class TreeNode:
def __init__(self, val):
self.val = val
self.left = self.right = None
class Codec:
def serialize(self, root):
"""Encodes a BST to a single string."""
def preorder(node):
if not node:
return []
return [str(node.val)] + preorder(node.left) + preorder(node.right)
return ','.join(preorder(root))
def deserialize(self, data):
"""Decodes your encoded data to BST."""
if not data:
return None
preorder = list(map(int, data.split(',')))
def helper(lower=float('-inf'), upper=float('inf')):
if preorder and lower < preorder[0] < upper:
val = preorder.pop(0)
root = TreeNode(val)
root.left = helper(lower, val)
root.right = helper(val, upper)
return root
return None
return helper()
Explanation
- Serialization: The
preorderfunction traverses the BST in preorder, converting node values to strings and joining them with commas. - Deserialization: The
helperfunction uses the BST property to rebuild the tree, popping values from the preorder list and ensuring they fit within the valid range for each subtree.
Time and Space Complexity
- Time Complexity: O(N), where N is the number of nodes.
- Space Complexity: O(N), due to the recursion stack and storage for the serialized string.
2. Relation Between Log Likelihood Loss for Logistic Regression and Maximum Likelihood Estimation (ML Engineer at LinkedIn)
Understanding the Problem
Logistic regression is a fundamental classification algorithm. Interviewers often ask about the connection between its loss function and the principle of Maximum Likelihood Estimation (MLE).
Key Concepts
- Logistic Regression: Models the probability that a binary outcome variable $y$ is 1 using the sigmoid function.
- Likelihood Function: Measures how likely the observed data is, given model parameters.
- Log Likelihood: The natural logarithm of the likelihood, often maximized for parameter estimation.
Mathematical Formulation
Given data points $x_i$ and labels $y_i \in \{0,1\}$, the probability for logistic regression is:
$ P(y_i = 1 | x_i) = \sigma(w^T x_i + b) = \frac{1}{1 + e^{-(w^T x_i + b)}} $
The likelihood of observing the data is:
$ L(w, b) = \prod_{i=1}^N P(y_i | x_i) $
For a binary classification:
$ L(w, b) = \prod_{i=1}^N [\sigma(w^T x_i + b)]^{y_i} [1 - \sigma(w^T x_i + b)]^{1-y_i} $
The log likelihood becomes:
$ \ell(w, b) = \log L(w, b) = \sum_{i=1}^N \left[ y_i \log \sigma(w^T x_i + b) + (1 - y_i) \log (1 - \sigma(w^T x_i + b)) \right] $
The negative log likelihood (NLL) is often minimized (equivalent to maximizing the log likelihood):
$ \text{NLL}(w, b) = - \ell(w, b) $
Connection to Loss Function
- The log likelihood loss for logistic regression is simply the negative log likelihood (NLL) above, also known as binary cross-entropy loss.
- Training logistic regression is equivalent to performing maximum likelihood estimation (MLE) of the model parameters.
Key Takeaways
- Maximizing log likelihood = minimizing the negative log likelihood loss used in logistic regression.
- MLE provides a statistical foundation for why this loss function is used.
Practical Example
import numpy as np
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def log_likelihood_loss(y_true, y_pred_proba):
eps = 1e-15 # To avoid log(0)
y_pred_proba = np.clip(y_pred_proba, eps, 1 - eps)
return -np.mean(y_true * np.log(y_pred_proba) + (1 - y_true) * np.log(1 - y_pred_proba))
# Example usage
X = np.array([[0.5, 1.5], [1, 2], [1.5, 0.5]])
y = np.array([0, 1, 1])
w = np.array([1, 1])
b = -1
probas = sigmoid(np.dot(X, w) + b)
loss = log_likelihood_loss(y, probas)
print('Log Likelihood Loss:', loss)
3. Find the Rarest Number in a List with nlogn Complexity (ML Engineer at Arm)
Understanding the Problem
Given a list of numbers, you must efficiently find the number that occurs the fewest times (the rarest number), in $O(n \log n)$ time.
Key Concepts
- Time Complexity: The naive solution uses a hash map (dictionary) for $O(n)$ time, but the question asks for $O(n \log n)$ — likely meaning you should use sorting.
- Sorting: Sort the numbers first, then count frequencies in a single pass.
Solution Approach
- Sort the list: $O(n \log n)$
- Count frequencies in a single pass: $O(n)$
Python Implementation
def rarest_number(nums):
if not nums:
return None # or raise exception
nums.sort()
min_count = float('inf')
rarest = None
i = 0
n = len(nums)
while i < n:
count = 1
while i + count < n and nums[i + count] == nums[i]:
count += 1
if count < min_count:
min_count = count
rarest = nums[i]
i += count
return rarest
# Example usage
lst = [4, 2, 2, 3, 3, 3, 1]
print(rarest_number(lst)) # Output: 4 or 1 (either is rarest with count 1)
Explanation
- After sorting, identical numbers are consecutive, making it easy to count their frequency in one pass.
- Keep track of the smallest frequency and update the rarest number.
- Time complexity is dominated by sorting: $O(n \log n)$.
Alternative: What if Hash Map is Allowed?
If you are allowed to use extra space, a hash map (dictionary) could solve the problem in $O(n)$ time, but the interview may want to assess your sorting-based approach.
4. Lexicographic Order with Custom Ordering (ML Engineer at Meta)
Understanding the Problem
Given a list of words and a custom ordering of the alphabet, determine if the words are sorted lexicographically according to that ordering. For example:
- Words: ["cat", "bat", "mat"]
- Ordering: ['c', 'b', 'a', 't']
Key Concepts
- Lexicographic (Dictionary) Order: Words are compared character by character using the specified order.
- Custom Ordering: Map each character to its position in the custom order for comparison.
Solution Approach
- Create a mapping from character to its index in the custom order.
- Compare each pair of adjacent words according to this mapping.
Python Implementation
def is_sorted(words, ordering):
order_map = {char: idx for idx, char in enumerate(ordering)}
def compare(word1, word2):
# Compare characters one by one
for c1, c2 in zip(word1, word2):
if order_map[c1] < order_map[c2]:
return True
elif order_map[c1] > order_map[c2]:
return False
# If all common chars are equal, shorter word comes first
return len(word1) <= len(word2)
for i in range(len(words) - 1):
if not compare(words[i], words[i+1]):
return False
return True
# Example usage
words = ["cat", "bat", "mat"]
ordering = ['c','b','a','t','m']
print(is_sorted(words, ordering)) # Output: True or False
Explanation
order_mapassigns an index to each character for fast lookup.- Each pair of words is compared character by character using the custom order.
- If a difference is found, the relative order is determined; if all compared characters are the same, the shorter word comes first.
Example Scenarios
| Words | Ordering | Is Sorted? | Explanation |
|---|---|---|---|
| ["cat", "bat", "mat"] | [c, b, a, t, m] | True | 'c' comes before 'b', so "cat" < "bat"; "bat" < "mat" |
| ["bat", "cat", "mat"] | [b, c, a, t, m] | True | 'b' < 'c', so "bat" < "cat"; "cat" < "mat" |
| ["mat", "cat", "bat"] | [m, c, b, a, t] | True | 'm' < 'c', so "mat" < "cat"; "cat" < "bat" |
| ["cat", "mat", "bat"] | [c, m, b, a, t] | False | 'c' < 'm', so "cat" < "mat"; but 'm' > 'b', so "mat" > "bat" |
Summary Table: Interview Questions and Concepts
| Company | Question | Main Concepts Tested | Key Skills Demonstrated |
|---|---|---|---|
| Quora | Serialize/Deserialize BST | Tree traversal, recursion, BST properties | Data structure manipulation, algorithmic thinking |
| Log Likelihood Loss and MLE | Probability, optimization, statistical learning theory | Mathematicallearning, loss function interpretation | |
| Arm | Find rarest number in a list (O(n log n)) | Sorting, frequency counting, algorithm complexity | Algorithm design, complexity analysis |
| Meta | Lexicographic order with custom ordering | String comparison, mapping, lexicographic order | Data manipulation, custom sorting logic |
Further Reading and Practice
- LeetCode Tree Problems
- Coursera: Machine Learning by Andrew Ng
- Scikit-learn: Logistic Regression
- InterviewBit: Tree Programming Exercises
- Sorting Algorithms in Python
Best of luck in your ML Engineer interview journey!
Related Articles
- Quant Research Interview Questions - Citadel and Five Rings
- Top Logistic Regression Questions in Netflix Data Scientist Interviews
- Abu Dhabi Investment Authority Interview (ADIA) - QRD Team
- Quant Finance and Data Science Interview Guide: Top Prep Tips
- Top Quant Finance Interview Questions and Prep Tips for 2026