PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates algorithmic problem-solving, data structure design, probabilistic reasoning for weighted random selection, sliding-window string processing for anagram detection, and streaming/online statistical computation.

  • medium
  • Meta
  • Coding & Algorithms
  • Machine Learning Engineer

Solve sampling and streaming tasks

Company: Meta

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Implement the following algorithmic tasks: 1. **Weighted random selection**: You are given an array of positive integer weights `w`, where `w[i]` is the relative probability of selecting index `i`. Design a class with: - a constructor that preprocesses `w` - a method `pickIndex()` that returns an index such that the probability of returning `i` is `w[i] / sum(w)` Aim for fast repeated sampling. 2. **Find anagram windows**: Given two strings `s` and `p`, return all starting indices in `s` where the substring of length `len(p)` is an anagram of `p`. 3. **Streaming mean**: Design a data structure for a stream of numbers that supports: - `add(x)`: add a new number to the stream - `getMean()`: return the arithmetic mean of all numbers seen so far Discuss edge cases such as an empty stream and large input volume.

Quick Answer: This question evaluates algorithmic problem-solving, data structure design, probabilistic reasoning for weighted random selection, sliding-window string processing for anagram detection, and streaming/online statistical computation.

Deterministic Weighted Selection

Given positive weights and deterministic draw values, return the weighted index each draw selects.

Constraints

  • draws are deterministic stand-ins for random integers in [0,total).

Examples

Input: ([1, 3, 2], [0, 1, 3, 4, 5])

Expected Output: [0, 1, 1, 2, 2]

Explanation: Cumulative buckets.

Input: ([5], [0, 99])

Expected Output: [0, 0]

Explanation: Single weight.

Hints

  1. Precompute prefix sums and binary-search each draw.

Find Anagram Windows

Return all start indices where a substring of s is an anagram of p.

Examples

Input: ('cbaebabacd', 'abc')

Expected Output: [0, 6]

Explanation: Classic case.

Input: ('abab', 'ab')

Expected Output: [0, 1, 2]

Explanation: Overlapping windows.

Input: ('abc', 'abcd')

Expected Output: []

Explanation: Pattern longer than text.

Streaming Mean

Process add/mean operations and return the arithmetic mean at each query.

Examples

Input: ((('mean',), ('add', 2), ('add', 4), ('mean',)),)

Expected Output: [None, 3.0]

Explanation: Empty then non-empty stream.

Input: ((('add', 1.5), ('mean',)),)

Expected Output: [1.5]

Explanation: Float value.

Last updated: Jun 27, 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
  • AI Coding 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

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)