PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCareers
|Home/Coding & Algorithms/Pinterest

Sample a string by real-valued scores

Last updated: Apr 13, 2026

Quick Overview

This question evaluates probabilistic sampling, numerical stability, and algorithmic implementation skills for weighted selection under floating-point edge cases, and is categorized in Coding & Algorithms for a Machine Learning Engineer role.

  • hard
  • Pinterest
  • Coding & Algorithms
  • Machine Learning Engineer

Sample a string by real-valued scores

Company: Pinterest

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given: - A list of strings `texts` (e.g., user comments), length `n`. - A list of floating-point scores `scores` of length `n`, where each score can be any real value, including `-∞` or `+∞`. Design an algorithm/function that **randomly returns one string** from `texts` such that the probability of returning `texts[i]` is proportional to the score distribution. Define the sampling rule explicitly as follows: - If all scores are finite, sample index `i` with probability \[ p_i = \frac{e^{scores[i]}}{\sum_{j=0}^{n-1} e^{scores[j]}}\quad\text{(softmax)}. \] - If one or more scores are `+∞`, return one of the `+∞` items uniformly at random. - If all scores are `-∞` (so all weights are zero), return any item uniformly at random. Requirements: - Time complexity: **O(n)** per sample. - Must be numerically stable for large/small scores. - Clearly state how you handle corner cases (empty input, NaN scores if they appear, etc.).

Quick Answer: This question evaluates probabilistic sampling, numerical stability, and algorithmic implementation skills for weighted selection under floating-point edge cases, and is categorized in Coding & Algorithms for a Machine Learning Engineer role.

Related Interview Questions

  • Design Hierarchical Permission Checks - Pinterest (medium)
  • Implement weighted random choice - Pinterest (medium)
  • Solve five hard algorithm problems - Pinterest
  • Find First Prefix-Matching Word - Pinterest (medium)
  • Solve Expression and Tree-List Problems - Pinterest (medium)
Pinterest logo
Pinterest
Mar 1, 2026, 12:00 AM
Machine Learning Engineer
Technical Screen
Coding & Algorithms
8
0
Loading...

You are given:

  • A list of strings texts (e.g., user comments), length n .
  • A list of floating-point scores scores of length n , where each score can be any real value, including -∞ or +∞ .

Design an algorithm/function that randomly returns one string from texts such that the probability of returning texts[i] is proportional to the score distribution.

Define the sampling rule explicitly as follows:

  • If all scores are finite, sample index i with probability

pi=escores[i]∑j=0n−1escores[j](softmax).p_i = \frac{e^{scores[i]}}{\sum_{j=0}^{n-1} e^{scores[j]}}\quad\text{(softmax)}.pi​=∑j=0n−1​escores[j]escores[i]​(softmax).

  • If one or more scores are +∞ , return one of the +∞ items uniformly at random.
  • If all scores are -∞ (so all weights are zero), return any item uniformly at random.

Requirements:

  • Time complexity: O(n) per sample.
  • Must be numerically stable for large/small scores.
  • Clearly state how you handle corner cases (empty input, NaN scores if they appear, etc.).

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Pinterest•More Machine Learning Engineer•Pinterest Machine Learning Engineer•Pinterest Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • Careers
  • 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.