Split a Lucky Red Packet Into Random Shares

Last updated: November 29, 2026

Quick Overview

Given a red packet total in cents and a recipient count, return a random split where every recipient gets at least one cent, the shares sum exactly to the total, and no early grabber can drain the packet.

Tencent
Coding & Algorithms
Software Engineer
Tencent
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given a red packet total in cents and a recipient count, return a random split where every recipient gets at least one cent, the shares sum exactly to the total, and no early grabber can drain the packet.

WeChat Pay's lucky red packets (hongbao) are a cultural fixture and spike enormously around Spring Festival, so a payments-flavoured warm-up like this fits a backend phone screen. The interviewer checks integer money handling, invariants (exact sum, minimum per person) and whether the candidate thinks about fairness of the distribution rather than just correctness.

What the Interviewer Expects
  • Works in integer cents, never floats
  • Guarantees every share is at least the minimum and the sum is exact
  • Explains why naive uniform splitting lets early grabbers take too much
  • Uses a bounded cap such as the double-mean rule
  • Validates impossible inputs (total smaller than n times minimum)
Key Topics to Cover
Randomized algorithms
Integer money arithmetic
Invariants
Fairness of distributions
Input validation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you make the split reproducible for audit given a packet ID?
  • Should shares be computed at send time or at grab time, and what changes for concurrency?
  • What is the expected value of each position under the double-mean rule?
  • How would you test fairness statistically?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: total_cents, n recipients, optional min_cents (default 1). Output: a list of n integers, each at least min_cents, summing exactly to total_cents. Shares should look random but no si...

High-Level Design

Pure uniform sampling of each share from [min, remaining] is badly skewed: the first grabber's expected take is about half the packet. The common fix is the double-mean rule: with remaining mo...


Submit Your Answer
Markdown supported

Related Questions