FastPrepWeighted Random Key Selection
Problems/Weighted Random Key Selection

Weighted Random Key Selection

Roblox logoRoblox● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks for a random pick of a map key in proportion to integer values, and gives a 5:2:2:1 example. We chose positive weights, ticket ranges in input order, and repeated draws, so the result is fixed. Our version is about 85% the same as the reported question.

You are given two matching arrays keys and weights. Every key is distinct and its positive integer weight determines its probability of selection.

For this exercise, assume a random draw is supplied as a zero-based integer ticket from 0 through sum(weights) - 1. Assign consecutive ticket ranges to keys in input order, giving each key a range whose length equals its weight. A uniform ticket therefore selects a key with probability proportional to its weight.

You are also given tickets, a list of independent draws. Return the selected key for each ticket in the same order. Each draw uses the original weights, so the same key may be selected repeatedly.

Function

selectWeightedKeys(keys: String[], weights: int[], tickets: int[]) → String[]

Examples

Example 1

keys = ["cat","dog","human","baby"]weights = [5,2,2,1]tickets = [0,4,5,6,7,8,9]return = ["cat","cat","dog","dog","human","human","baby"]

The source weights give ten tickets: cat owns 0..4, dog owns 5..6, human owns 7..8, and baby owns 9.

Example 2

keys = ["red","blue"]weights = [1,3]tickets = [3,0,1,1]return = ["blue","red","blue","blue"]

The last three tickets belong to blue. Repeating ticket 1 repeats the selected key.

Example 3

keys = ["solo"]weights = [7]tickets = []return = []

An empty batch contains no draws and returns an empty result.

Constraints

  • 1 <= keys.length = weights.length <= 20000.
  • Keys are distinct strings containing 1 to 20 characters. Each character is an English letter, a digit, an underscore, a hyphen, or a period.
  • The total number of characters across all keys is at most 100000.
  • 1 <= weights[i] and sum(weights) <= 2 * 10^9.
  • 0 <= tickets.length <= 500.
  • 0 <= tickets[i] < sum(weights).

More Roblox Onsite Interview questions

  • Most Frequent Call Path From Function Trace LogsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Design Search Autocomplete SystemPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Candy Crush Grid Matching and GravityPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Topological Sort with Secondary OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Most Frequent Call Stack Per ThreadPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Count and Score Distinct Hat AssignmentsONSITE INTERVIEW · Seen Sep 2026
  • Rate Limiter Sliding Window With Per-Entity LimitsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Most Frequent Call Path in Interleaved LogsONSITE INTERVIEW · Seen Aug 2026
  • Grid Pathfinding with Obstacles (DFS)PHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2026
  • Maximize Distance to Closest Person — Return the SeatONSITE INTERVIEW · Seen Jun 2026

Similar questions at other companies

  • Totals from a Tiered Rate TableMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Farthest Affordable Shop per QueryAdobe · ONSITE INTERVIEWAdobe interview questions
  • Random Pick with Weight Using Recorded DrawsAmazon · ONSITE INTERVIEW
  • Multinomial Sampling with Distribution ChecksNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
  • Shortest Subarray with Sum at Least KOracle · ONSITE INTERVIEWOracle interview questions
  • Roll a Weighted DieLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Count Lamps Lighting Each PointAirbnb · OAAirbnb interview questions
  • Choose ContainersIMC Trading · OAIMC Trading interview questions
  • Good Ways to Split an ArraySalesforce · PHONE SCREENSalesforce interview questions

Interview reports that mention this question

  • Roblox New Grad Engineer Rounds: 13 Experiences (Part 6 of 7)

Roblox interview experiences

  • Roblox OA: What It Tests (2026)
  • Roblox New Grad Early OA Tasks: 10 Experiences (Part 7 of 7)
  • Roblox New Grad HackerRank OA: 18 Experiences (Part 5 of 7)
  • Roblox New Grad CodeSignal Finals: 23 Experiences (Part 4 of 7)
  • Roblox New Grad Games and BQ: 25 Experiences (Part 3 of 7)
All Roblox interview questionsSee Roblox hiring insights
Saved
class Solution {
    public String[] selectWeightedKeys(String[] keys, int[] weights, int[] tickets) {
        // write your code here
    }

}
Checking account…