PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in probability and numerical methods, focusing on sampling from truncated distributions and analytical properties of estimators under various loss functions.

  • Medium
  • Google
  • Coding & Algorithms
  • Data Scientist

Implement Sampling and Minimize Loss in Numerical Coding

Company: Google

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

##### Scenario Numerical coding challenges on sampling and loss minimization. ##### Question a) Implement functions to sample from truncated normal distributions for x>1, 4<x<4.05, and x>4. b) For an array X, find the value minimizing Σ(x−θ)², then the value minimizing Σ|x−θ|, and derive the loss that yields the 90th percentile. ##### Hints Use rejection or CDF-inverse methods; derivatives show mean, median, and quantile solutions.

Quick Answer: This question evaluates proficiency in probability and numerical methods, focusing on sampling from truncated distributions and analytical properties of estimators under various loss functions.

Given an array X of n real numbers and a mode, return the value θ that minimizes a specified loss. Modes: (1) 'L2': minimize Σ(xi−θ)^2; return the arithmetic mean of X. (2) 'L1': minimize Σ|xi−θ|; return the smallest median (lower median when n is even). (3) 'quantile': return the τ-quantile, defined as the smallest θ such that at least τ fraction of elements are ≤ θ (nearest-rank/left quantile). If X is empty, raise ValueError. For mode 'quantile', τ must satisfy 0 < τ ≤ 1 (default τ = 0.9).

Constraints

  • 1 <= n <= 200000
  • |xi| <= 1e9
  • mode in {'L2','L1','quantile'}
  • For 'quantile' mode: 0 < tau <= 1
  • Return the smallest minimizer if multiple minimizers exist

Hints

  1. For L2, set derivative of Σ(x−θ)^2 to zero to get θ = mean(X).
  2. For L1, any median minimizes the sum of absolute deviations; choose the lower median for determinism.
  3. For the τ-quantile, use k = ceil(τ*n) - 1 on the sorted order; compute k-th order statistic via Quickselect to avoid full sorting.
Last updated: Mar 29, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Infection Spread on a Grid (Cellular Automaton) - Google (hard)
  • Most Active Users in a Live Communication Stream - Google (medium)
  • Boolean Expression Tree with Leaf Flips - Google (medium)
  • Streaming Points: Remove Any Pair Within a Distance - Google (medium)
  • Solve Rooms and Top-K Streams - Google (medium)