Solve queue switch and coin change puzzles
Company: Jane Street
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
This entry contains two separate interview-style quantitative/algorithmic questions.
---
## 1. Queue switching decision problem
You are standing in a single FIFO queue of 100 customers waiting for service at a checkout counter. Customers are served one at a time, and each service takes the same fixed amount of time (say, one time unit). Positions are numbered `1` (front of the line, served next) through `100` (back of the line).
At time 0, a second identical checkout counter opens. Each of the other 99 customers independently decides to move to the new queue with probability 1/2. All those who choose to move (possibly including you) are randomly ordered in the new queue; every ordering of the movers is equally likely. Both checkout counters start serving at the same time and serve at the same rate (one customer per time unit) from their respective queues.
You are currently at position `k` in the original queue (1 ≤ k ≤ 100). You must decide **once** at time 0 whether to stay in the original queue or move to the new one.
1. Derive an expression for your **expected waiting time** until service if you **stay** in the original queue as a function of `k`.
2. Derive an expression for your **expected waiting time** until service if you **switch** to the new queue, taking into account that:
- Each of the other 99 customers moves independently with probability 1/2.
- If you move, your position among all movers is uniformly random.
- Both queues are then served in parallel at the same rate.
3. For which values of `k` is it **better in expectation** (i.e., gives a lower expected waiting time) to switch rather than stay? Give the threshold value(s) of `k` where your preference changes.
You may assume that everyone else’s decision to move or stay is independent of your own and of their position; you are not required to model strategic behavior.
---
## 2. Coin change combinations with US-style denominations
You are given an integer amount `n` representing a value in **cents** and you have an unlimited supply of the following coin denominations:
- 1 cent
- 5 cents
- 10 cents
- 25 cents
- 100 cents (i.e., 1 dollar)
You want to know in how many **distinct combinations** (multisets) you can make exactly `n` cents using these coins. The **order of coins does not matter**; for example, `1 + 5` is the same combination as `5 + 1`.
**Task:**
1. Define the problem formally in terms of counting integer solutions.
2. Design an algorithm that, given `n` (say up to 10,000), returns the number of distinct combinations of coins whose total value is exactly `n`.
3. Analyze the time and space complexity of your algorithm.
You do **not** need to print the actual combinations, only their count.
Quick Answer: This two-part question evaluates probabilistic reasoning about expected waiting times with random queue switching and combinatorial/algorithmic counting for coin-change combinations, testing skills in probability, expectation and order statistics as well as combinatorics and dynamic programming within the Coding & Algorithms domain.
Part 1: Expected wait when choosing between two queues
There are n customers in a single FIFO checkout line, and you are initially at position k, where 1 is the front of the line. At time 0, a second identical checkout opens. Each of the other n - 1 customers independently switches to the new checkout with probability 1/2. If you stay, the original line keeps the same relative order among customers who remain. If you switch, you join all movers and your position in the new line is uniformly random among them. Both checkouts begin serving at the same time and at the same rate, one customer per unit time. Waiting time means the number of customers served before your own service begins. Write a function that returns [expected_wait_if_stay, expected_wait_if_switch, decision_code, switch_threshold], where decision_code is -1 if staying is better, 0 if both choices are equally good, and 1 if switching is better. The value switch_threshold is the smallest starting position t such that switching is strictly better for every position k >= t, or -1 if no such t exists. The original interview setting is the special case n = 100.
Constraints
- 1 <= n <= 1000000000
- 1 <= k <= n
- Do not simulate random outcomes; compute the exact expectations directly.
Examples
Input: (100, 1)
Expected Output: [0.0, 24.75, -1, 51]
Explanation: At the front, staying gives zero wait. In the 100-customer version, switching first becomes strictly better at position 51.
Input: (100, 50)
Expected Output: [24.5, 24.75, -1, 51]
Explanation: Expected wait if staying is (50 - 1) / 2 = 24.5, still slightly better than switching.
Input: (100, 51)
Expected Output: [25.0, 24.75, 1, 51]
Explanation: This is the first position where switching is better in expectation.
Input: (5, 3)
Expected Output: [1.0, 1.0, 0, 4]
Explanation: For n = 5 and k = 3, both choices have the same expected wait.
Input: (1, 1)
Expected Output: [0.0, 0.0, 0, -1]
Explanation: Edge case: there are no other customers, so both choices are equivalent and switching is never strictly better.
Solution
def solution(n, k):
stay_expected = (k - 1) / 2.0
switch_expected = (n - 1) / 4.0
if stay_expected < switch_expected:
decision_code = -1
elif stay_expected > switch_expected:
decision_code = 1
else:
decision_code = 0
switch_threshold = (n + 1) // 2 + 1
if switch_threshold > n:
switch_threshold = -1
return [stay_expected, switch_expected, decision_code, switch_threshold]
Time complexity: O(1). Space complexity: O(1).
Hints
- If you stay, only the customers originally ahead of you can delay you, and each of them remains in your line with probability 1/2.
- If M other customers switch with you, your position among the M + 1 movers is uniformly random, so the expected number of movers ahead of you is the average of 0 through M.
Part 2: Count coin change combinations with US-style denominations
Given an integer n representing cents, count how many distinct combinations of coins sum exactly to n using unlimited coins of denominations 1, 5, 10, 25, and 100. Order does not matter, so combinations are multisets rather than sequences. Formally, count the number of nonnegative integer solutions to a + 5b + 10c + 25d + 100e = n.
Constraints
- 0 <= n <= 10000
- You have an unlimited supply of 1, 5, 10, 25, and 100 cent coins
- The answer fits in Python's built-in integer type
Examples
Input: 0
Expected Output: 1
Explanation: Edge case: there is exactly one way to make 0 cents, by choosing no coins.
Input: 5
Expected Output: 2
Explanation: The combinations are 1+1+1+1+1 and 5.
Input: 10
Expected Output: 4
Explanation: The combinations are 10 pennies, one nickel and 5 pennies, two nickels, and one dime.
Input: 30
Expected Output: 18
Explanation: There are 18 distinct multisets using 1, 5, 10, and 25 cent coins that total 30.
Input: 100
Expected Output: 243
Explanation: This includes all ways to make 100 cents without the dollar coin, plus the single combination consisting of one 100-cent coin.
Solution
def solution(n):
coins = [1, 5, 10, 25, 100]
dp = [0] * (n + 1)
dp[0] = 1
for coin in coins:
for amount in range(coin, n + 1):
dp[amount] += dp[amount - coin]
return dp[n]
Time complexity: O(n). Space complexity: O(n).
Hints
- Because order does not matter, process coin types one at a time instead of trying all coin sequences.
- Let dp[x] be the number of ways to make x cents using only the coin types processed so far.