Skip to content

Cookies on Jobverse

We use cookies that are needed to sign you in and keep the site working. With your permission we’d also use an analytics cookie to count job views, and Google advertising cookies to show ads. The site works the same whatever you choose. Read the Cookie Policy.

Jobverse
Sign in

AI Engineer Top 100 Interview Questions

100 AI engineer interview questions with structured answers, comparison tables, flow diagrams, worked calculations and runnable code. Covers machine learning fundamentals, evaluation, deep learning, language models, retrieval, agents, deployment and responsible AI, from the basics to system design.

100 questions

Questions 21-40 of 100

  1. Q21.How do linear regression and logistic regression differ?

    Linear regression predicts a numerical quantity using a weighted combination of features. Binary logistic regression transforms such a combination into a probability for classification.

    Linear regressionLogistic regression
    Predicts a numerical outcomePredicts class membership
    Outputs an unbounded fitted valueOutputs a probability between zero and one
    Usually trained with squared errorUsually trained with binary cross-entropy

    When to use which:

    • Use linear regression for a numerical target when its assumptions and error behavior make it a useful baseline for the intended prediction.
    • Use logistic regression for classification when a compact, interpretable probability model is useful, and select the decision threshold separately from fitting.
    • Use regularization and appropriate feature transformations with either method, evaluating whether linear relationships in the chosen representation are sufficiently expressive.

    The name of a method does not determine the task category; inspect its output, loss, and intended target.

  2. Q22.How does a decision tree choose a split?

    A decision tree selects a feature and boundary that make the resulting groups more consistent with the target. The choice is usually greedy rather than globally optimal.

    • Candidate splits: consider eligible boundaries or category partitions, subject to constraints such as minimum child size and permitted depth.
    • Classification objective: measure how much a split reduces weighted class impurity, using an appropriate impurity criterion for the implementation.
    • Regression objective: measure the reduction in within-group target error, often using squared deviations from each group’s predicted value.
    • Complexity control: constrain depth, leaf size, or split improvement, or prune afterward, because unrestricted trees can memorize small groups of training examples.

    Evaluate the final tree on held-out data because a strong local split does not guarantee useful overall generalization.

  3. Q23.What is the difference between bagging and boosting?

    Bagging combines models fitted to varied versions of the data. Boosting builds an ensemble whose later components improve the predictions of the existing combination.

    BaggingBoosting
    Fits components largely independentlyFits components in sequence, each depending on the last
    Mainly reduces prediction varianceMainly corrects residual errors
    Tuning focus: diversity and component strengthTuning focus: learning rate and complexity

    When to use which:

    • Use bagging when individual models are unstable and averaging diverse predictions can improve robustness without requiring a tightly coupled training sequence.
    • Use boosting when sequential improvements capture useful structure, while carefully tuning model complexity, update strength, and stopping to limit overfitting.
    • Compare both on representative data rather than assuming one family wins, and include model size, prediction latency, and operational complexity in the decision.

    An ensemble can still learn leakage or biased labels, so combining more models does not replace sound data preparation.

  4. Q24.What assumptions and limitations matter in centroid-based clustering?

    Centroid-based clustering groups observations around representative centers, usually using a distance objective. Its usefulness depends on whether that geometry matches meaningful groups in the data.

    • Representation: feature units and scaling affect distance, so unrelated magnitude differences can dominate assignments unless the representation is chosen deliberately.
    • Shape: compact groups are easier to represent than curved, irregular, or strongly unequal-density structures that do not align with nearest-center boundaries.
    • Initialization: different starting centers can produce different local solutions, so multiple initializations and stability checks are useful.
    • Interpretation: a chosen number of clusters forces a partition even when distinct natural groups are absent; validate practical usefulness and avoid treating clusters as ground-truth categories.
  5. Q25.What does principal component analysis preserve, and what can it lose?

    Principal component analysis creates orthogonal directions that preserve as much variance as possible in a lower-dimensional linear representation. Preserved variance is not the same as preserved predictive value.

    • Preparation: center the data and decide whether feature scaling is appropriate, since units can strongly influence which directions carry most variance.
    • Compression: retain a subset of components to reduce storage, computation, or redundant variation, while measuring the effect on downstream performance.
    • Limitations: a useful target signal may lie in a low-variance direction and disappear even when the retained variance percentage looks high.
    • Evaluation: fit the transformation only on training records and examine interpretability, robustness, and task performance instead of selecting components solely from a convenient percentage.
  6. Q26.How do absolute-value and squared-weight regularization differ?

    Both discourage overly complex fitted weights by adding a penalty to the training objective. Their different geometry produces different patterns of coefficient shrinkage.

    Absolute-value penaltySquared-weight penalty
    Adds the sum of absolute weightsAdds the sum of squared weights
    Can push coefficients to exactly zeroShrinks coefficients smoothly
    Tends toward a sparse modelSpreads weight across features

    When to use which:

    • Use an absolute-value penalty when a sparse set of useful features is desirable, while checking stability when several features contain similar information.
    • Use a squared-weight penalty when retaining correlated signals and controlling coefficient magnitude is more useful than forcing many coefficients to zero.
    • Tune penalty strength with validation and scale features appropriately, because coefficient penalties otherwise depend heavily on arbitrary measurement units.

    Combining both penalties is possible, but the choice should follow measured behavior rather than a blanket preference for sparsity.

  7. Q27.Why can a model’s training loss improve while the business metric gets worse?

    The training loss supplies a convenient optimization signal, while the business metric measures the outcome users need. These objectives can diverge, especially on unseen or shifted data.

    Training lossBusiness evaluation metric
    Guides parameter updatesMeasures practical success
    Should be easy to optimizeShould match user outcomes
    Computed on training predictionsComputed on held-out or deployed decisions

    When to use which:

    • Use a suitable training loss to make learning feasible, but validate that improvements translate into the desired decisions and operating constraints.
    • Use the business metric to compare complete systems, including thresholds, ranking policies, review stages, and any downstream consequences of predictions.
    • Investigate divergence through error slices, distribution changes, overfitting, and objective mismatch before assuming that more training will resolve the problem.

    A lower loss is evidence about the optimization objective, not an automatic guarantee of a better product.

  8. Q28.How would you tune hyperparameters with a limited compute budget?

    I would define a reliable evaluation procedure before searching configurations. The goal is a robust improvement within a budget, not the best accidental score from many trials.

    1. Select a representative validation strategy and baseline, then identify the few configuration choices most likely to affect quality or operating cost.
    2. Set a trial budget and use a sensible search range. Start broadly, then spend additional effort on regions supported by observed results.
    3. Stop clearly unpromising runs when the stopping criterion is appropriate, and record data versions, seeds, settings, and resource consumption for every trial.
    4. Recheck leading configurations across suitable splits or repeated runs, assess cost and latency, and evaluate the final fixed choice on untouched data.
    flowchart TD
      A["Define search budget"]
      B["Run candidate"]
      C{"Promising result?"}
      D["Stop or deprioritize"]
      E["Validate robustness"]
      F{"Budget remaining?"}
      G["Select final candidate"]
      A --> B
      B --> C
      C -->|no| D
      C -->|yes| E
      D --> F
      E --> F
      F -->|yes| B
      F -->|no| G

    Trade-offs:

    • A broad search explores more possibilities, while repeated evaluation gives stronger evidence about fewer candidates and reduces sensitivity to lucky runs.
    • Aggressive early stopping saves resources but can discard configurations that improve more slowly, so validate the stopping rule itself.

    Example

    Suppose only 20 full training runs are affordable.

    1. Reserve several runs for confirming the strongest candidates.
    2. Explore a small set of influential settings first.
    3. Reject changes whose gain is smaller than observed run-to-run variation.
    4. Select a reproducible improvement that also meets the serving budget.
  9. Q29.How do you calculate mean absolute error and root mean squared error?

    Both summarize numerical prediction errors in the target’s units. Mean absolute error treats error magnitude linearly, while root mean squared error emphasizes larger misses more strongly.

    MAERMSE
    Averages absolute errorsSquares errors before averaging
    Less sensitive to extreme errorsMore sensitive to extreme errors
    • Mean absolute error: MAE = sum(abs(error_i)) / n, where each error is prediction minus actual value and n is the example count.
    • Root mean squared error: RMSE = sqrt(sum(error_i ** 2) / n), which squares errors before averaging and taking the square root.
    • Choice: use the error cost and target distribution to decide which summary matters; reporting both can reveal a small number of large mistakes.
    • Caution: compare scores on the same population and inspect segments, since a strong overall value can hide poor performance on important cases.

    Example

    This calculation uses prediction errors of 1, 1, and 8 days.

    1. Absolute-error sum: 1 + 1 + 8 = 10.
    2. MAE = 10 / 3 ≈ 3.33 days.
    3. Squared-error sum: 1 + 1 + 64 = 66.
    4. RMSE = sqrt(66 / 3) = sqrt(22) ≈ 4.69 days.
  10. Q30.Why does the base rate matter when interpreting a positive prediction?

    A positive signal must be interpreted together with how common the underlying event is. Even a sensitive detector can generate mostly false alerts when the event is rare.

    • Conditional probability: P(A given B) = P(B given A) * P(A) / P(B), where A is the event and B is the observed signal.
    • Base rate: P(A) is the event frequency before observing the signal; ignoring it can greatly overstate the value of a positive result.
    • Signal frequency: the denominator includes signals from both genuine events and ordinary cases, not only successfully detected positives.
    • Use: calculate expected counts for a representative population when explaining alerts or planning review capacity, and revisit the estimate if prevalence changes.

    Example

    This calculation uses 10,000 messages, 1% unwanted messages, 90% detection, and a 10% false-positive rate.

    1. There are 100 unwanted and 9,900 ordinary messages.
    2. The detector flags 90 unwanted messages.
    3. It also flags 990 ordinary messages.
    4. The fraction of correct alerts is 90 / (90 + 990) ≈ 8.33%.
  11. Q31.How would you remove duplicate records by identifier while preserving the first occurrence?

    I would scan the records once, remember identifiers already encountered, and keep only the first record for each identifier. This preserves meaningful input order.

    • Identity rule: decide which field defines duplication before writing the code, because identical text and identical identifiers are different requirements.
    • Implementation: use membership checks against a set of previously observed identifiers, and append accepted records to a separate ordered result.
    • Complexity: expected processing time is linear in record count, with additional memory proportional to the number of unique identifiers retained.
    • Validation: define behavior for missing or unhashable identifiers and conflicting duplicates. Keeping the first record is a policy choice, not proof that it is authoritative.

    Example

    This code keeps the first record for each required integer identifier.

    def deduplicate(records):
        seen = set()
        result = []
        for record in records:
            identifier = record["id"]
            if identifier not in seen:
                seen.add(identifier)
                result.append(record)
        return result
    
    records = [
        {"id": 2, "value": "first"},
        {"id": 1, "value": "second"},
        {"id": 2, "value": "duplicate"},
    ]
    print([record["value"] for record in deduplicate(records)])
    # Expected output: ['first', 'second']
  12. Q32.How would you process an iterable in fixed-size batches without loading everything into memory?

    I would build one batch at a time and yield it when it reaches the requested size. The final incomplete batch should also be returned.

    • Input contract: accept an iterable rather than requiring a complete list, allowing records to arrive from a file, stream, or generator.
    • Memory: keep only the current batch inside the batching function. Downstream processing must also avoid collecting the entire stream into memory.
    • Edge cases: reject nonpositive batch sizes, produce no batches for empty input, and preserve the original order of the records.
    • Operational behavior: choose a batch size that balances processing overhead with memory and latency, and define how partial failures are retried without duplicating completed work.

    Example

    This code divides seven values into batches containing at most three values.

    def batches(values, size):
        if not isinstance(size, int) or isinstance(size, bool) or size <= 0:
            raise ValueError("size must be a positive integer")
        batch = []
        for value in values:
            batch.append(value)
            if len(batch) == size:
                yield batch
                batch = []
        if batch:
            yield batch
    
    for batch in batches(range(7), 3):
        print(batch)
    # Expected output:
    # [0, 1, 2]
    # [3, 4, 5]
    # [6]
  13. Q33.How would you implement cosine similarity between two embedding vectors?

    Cosine similarity divides the dot product by the product of the vector lengths. It measures directional alignment rather than treating vector magnitude as similarity.

    • Formula: similarity = sum(a_i * b_i) / (length_a * length_b), with each length calculated from the square root of squared components.
    • Preconditions: require nonempty vectors with the same dimension and finite numerical values. Similarity is undefined if either vector has zero length.
    • Complexity: a direct implementation needs linear time in vector dimension and constant extra working space beyond the input vectors.
    • Interpretation: use compatible embedding spaces and the metric expected by their training setup. A high score is not a calibrated probability of relevance or factual agreement.

    Example

    This code compares two small finite vectors and rejects undefined zero-length cases.

    from math import isfinite, sqrt
    
    def cosine(a, b):
        if not a or len(a) != len(b):
            raise ValueError("vectors must be nonempty and equally sized")
        if not all(isfinite(x) for x in a + b):
            raise ValueError("components must be finite")
        length_a = sqrt(sum(x * x for x in a))
        length_b = sqrt(sum(x * x for x in b))
        if length_a == 0 or length_b == 0:
            raise ValueError("zero vector has no direction")
        return sum(x * y for x, y in zip(a, b)) / (length_a * length_b)
    
    print(round(cosine([1.0, 1.0], [1.0, 0.0]), 3))
    # Expected output: 0.707
  14. Q34.How would you implement softmax without overflowing on large logits?

    I would subtract the largest logit before exponentiating, then divide each exponential by their sum. This preserves the mathematical probabilities while improving numerical stability.

    • Shift invariance: adding or subtracting the same constant from every logit cancels between numerator and denominator, leaving the resulting distribution unchanged.
    • Stability: after subtracting the maximum, every exponent is nonpositive, preventing overflow from large positive logits in ordinary finite inputs.
    • Validation: reject empty or nonfinite input and distinguish raw logits from values that have already been normalized into probabilities.
    • Interpretation: the output values sum to one for a mutually exclusive choice, but normalization alone does not make the model well calibrated or its predictions correct.

    Example

    This code normalizes large logits without directly exponentiating their original values.

    from math import exp, isfinite
    
    def softmax(logits):
        if not logits or not all(isfinite(x) for x in logits):
            raise ValueError("logits must be nonempty and finite")
        largest = max(logits)
        weights = [exp(x - largest) for x in logits]
        total = sum(weights)
        return [weight / total for weight in weights]
    
    print([round(x, 3) for x in softmax([1000.0, 1001.0, 1002.0])])
    # Expected output: [0.09, 0.245, 0.665]
  15. Q35.How would you select the top k scored items with deterministic tie-breaking?

    I would sort by descending score and then by a stable identifier before taking the first k items. Explicit tie-breaking makes results reproducible.

    • Contract: define whether larger scores are better, require a valid nonnegative integer k, and decide whether duplicate identifiers are allowed.
    • Ordering: use the stable identifier as the secondary key so that equal scores do not produce different results merely because input order changed.
    • Complexity: full sorting requires roughly O(n log n) time. For very large inputs and small k, a bounded selection structure can reduce work.
    • Correctness: validate scores before selection, apply required eligibility filters first, and remember that deterministic ordering does not establish the relevance of the scoring function.

    Example

    This code returns the highest-scored items, resolving equal scores by identifier.

    def top_items(items, k):
        if not isinstance(k, int) or isinstance(k, bool) or k < 0:
            raise ValueError("k must be a nonnegative integer")
        return sorted(items, key=lambda item: (-item["score"], item["id"]))[:k]
    
    items = [
        {"id": "b", "score": 0.8},
        {"id": "c", "score": 0.9},
        {"id": "a", "score": 0.8},
    ]
    print([item["id"] for item in top_items(items, 2)])
    # Expected output: ['c', 'a']
  16. Q36.What are the main components of a neural network?

    A neural network transforms inputs through parameterized layers to produce a prediction. Its architecture determines how information is represented, combined, and passed between layers.

    • Weights and biases: learned parameters control transformations of incoming representations, while the number and arrangement of parameters affect capacity and resource requirements.
    • Activations: nonlinear functions let stacked layers express relationships that a composition of purely linear transformations cannot capture.
    • Output representation: the final layer must match the task, such as independent labels, mutually exclusive categories, or a numerical prediction.
    • Training objective: a loss measures prediction error and provides the signal for learning, while held-out evaluation checks whether the learned behavior generalizes.

    Architecture, training data, and optimization interact, so adding depth alone does not guarantee better results.

  17. Q37.When should you use a rectified activation, a sigmoid, or softmax?

    These functions serve different roles in a network. Hidden-layer nonlinearity and output interpretation should be selected separately based on the prediction task.

    Rectified activationSigmoidSoftmax
    Outputs zero for negative inputsOutputs an independent value between zero and oneOutputs a vector that sums to one
    Used in hidden layersUsed for binary or multilabel outputsUsed for mutually exclusive classes

    When to use which:

    • Use a rectified hidden activation when its optimization behavior is suitable, while monitoring inactive units and considering architecture-specific alternatives.
    • Use separate sigmoid outputs when several labels can be true together, and choose a loss and decision thresholds consistent with that interpretation.
    • Use softmax when the output represents one choice among mutually exclusive classes, supplying logits or probabilities based on the loss implementation’s expectations.

    Avoid applying the same output transformation twice, and remember that a probability-shaped value is not automatically calibrated.

  18. Q38.What does backpropagation compute during a neural-network training step?

    Backpropagation computes derivatives of the loss with respect to model parameters by applying the chain rule. An optimizer then uses those derivatives to update parameters.

    1. Run a forward pass and retain the intermediate information needed to calculate derivatives through the operations that produced the prediction.
    2. Calculate a loss that matches the target and output representation, checking that masking and reduction choices reflect the intended examples.
    3. Propagate derivatives backward through the computation and accumulate parameter gradients, clearing previous gradients at the intended accumulation boundary.
    4. Inspect gradients for nonfinite values or unexpected absence before allowing an update, and evaluate learning progress on separate validation examples.
    flowchart TD
      A["Forward computation"]
      B["Calculate loss"]
      C["Compute derivatives"]
      D{"Gradients valid?"}
      E["Apply optimizer update"]
      F["Investigate numerical issue"]
      A --> B
      B --> C
      C --> D
      D -->|yes| E
      D -->|no| F
    • The gradient computation and the optimizer are distinct components; changing the optimizer does not change the chain rule itself.
    • Retaining intermediate activations supports differentiation but increases memory use, creating a practical trade-off between storage and recomputation.
  19. Q39.How do batch normalization and layer normalization differ?

    Both normalize intermediate activations, but they use different groups of values to calculate statistics. This difference affects batch dependence and training-versus-inference behavior.

    Batch normalizationLayer normalization
    Statistics across batch-related dimensionsStatistics across feature dimensions
    Depends on other examples in the batchIndependent of other examples
    Uses stored running statistics at inferenceUses the current example's statistics at inference

    When to use which:

    • Use batch normalization when its batch statistics are reliable and its behavior suits the architecture, taking care with tiny or unusual batches.
    • Use layer normalization when normalization should be independent of other examples, especially in sequence architectures with changing lengths or batch composition.
    • Verify which dimensions are normalized and how the implementation handles evaluation mode, rather than assuming all normalization layers behave identically.

    Normalization can help optimization, but it is not a substitute for representative data, appropriate learning rates, or correct evaluation.

  20. Q40.What does dropout do, and what changes at inference time?

    Dropout randomly masks selected activations during training to reduce dependence on particular paths through a network. Standard inference disables this random masking.

    • Regularization: the network must learn useful behavior across varying active subsets, which can reduce overfitting when the rate is appropriate.
    • Scaling: common implementations scale retained activations during training so that expected magnitudes align with evaluation behavior; check the actual convention used.
    • Configuration: too much dropout can make learning difficult or cause underfitting, while too little may offer limited benefit on a particular task.
    • Evaluation: verify the model’s mode before measuring quality or serving predictions, because accidentally active dropout can introduce unintended variability and misleading comparisons.

    Specialized uncertainty methods may deliberately retain stochastic masking, but that is a separate, explicitly evaluated design choice.