Implement Byte Formatting and Cafeteria Billing
Company: Upstart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
You need to solve two independent programming tasks.
### Task 1: Format a byte count
Given an integer `bytes` representing a size in bytes, return a formatted string using the most appropriate unit according to these rules:
- If `bytes < 1024`, use the unit `B`.
- If `1024 <= bytes < 1024 * 1024`, convert to kilobytes and use the unit `KB`.
- If `bytes >= 1024 * 1024`, convert to megabytes and use the unit `MB`.
- Use binary units: `1 KB = 1024 B` and `1 MB = 1024 KB`.
- For converted values, round and display exactly two digits after the decimal point. For plain bytes, display the integer value followed by `B`.
Example:
Input:
```text
12345
```
Output:
```text
12.06 KB
```
### Task 2: Compute cafeteria revenue
A buffet-style cafeteria has a fixed number of seats, `capacity`. You are given:
- `capacity`: the maximum number of customers who can be inside at the same time.
- `fees`: an array where `fees[i]` is the amount customer `i` must pay the first time they successfully enter the cafeteria.
- `records`: an array of customer IDs representing enter/exit events processed strictly in order.
For each customer ID in `records`:
- If the customer is currently not inside, the record means they attempt to enter.
- If the cafeteria is not full, they enter.
- If this is their first successful entry, add `fees[id]` to total revenue.
- If they have successfully entered before, they do not pay again.
- If the cafeteria is full, the entry attempt fails immediately. The customer does not enter, is not queued, and will not automatically retry.
- If the customer is currently inside, the record means they leave. Leaving is always valid for customers who are inside.
Assume all customer IDs in `records` are valid indices in `fees`. Return the total revenue collected.
Example:
```text
capacity = 2
fees = [1, 2, 3]
records = [0, 1, 0, 2, 1, 1, 1, 2]
```
Processing:
```text
0 enters: inside = {0}, revenue += 1, total = 1
1 enters: inside = {0,1}, revenue += 2, total = 3
0 leaves: inside = {1}, total = 3
2 enters: inside = {1,2}, revenue += 3, total = 6
1 leaves: inside = {2}, total = 6
1 re-enters: inside = {1,2}, revenue += 0, total = 6
1 leaves: inside = {2}, total = 6
2 leaves: inside = {}, total = 6
```
Output:
```text
6
```
Quick Answer: This question evaluates precision in numeric formatting and unit conversion for byte sizes, along with stateful simulation of capacity-constrained event processing and one-time fee accounting.
Part 1: Format a Byte Count
Given a non-negative integer bytes_count representing a number of bytes, return a formatted size string using binary units. Use these rules: values smaller than 1024 use B, values from 1024 up to but not including 1024 * 1024 use KB, and values at least 1024 * 1024 use MB. For KB and MB, divide by the unit size and format the number with exactly two digits after the decimal point. Return results in the form "12 B", "1.00 KB", or "3.50 MB".
Constraints
- 0 <= bytes_count <= 10^12
- Use binary units: 1 KB = 1024 B and 1 MB = 1024 KB
- Only the units B, KB, and MB are used
Examples
Input: (0,)
Expected Output: "0 B"
Explanation: Zero is below 1024, so it stays in bytes.
Input: (1023,)
Expected Output: "1023 B"
Explanation: Still below the KB threshold.
Input: (1024,)
Expected Output: "1.00 KB"
Explanation: Exactly 1024 bytes is exactly 1 KB.
Input: (12345,)
Expected Output: "12.06 KB"
Explanation: 12345 / 1024 = 12.055664..., which rounds to 12.06 KB.
Input: (1048576,)
Expected Output: "1.00 MB"
Explanation: 1048576 bytes is exactly 1 MB.
Hints
- Compare the value against 1024 and 1024 * 1024 in that order.
- In Python, an f-string with :.2f is a simple way to keep exactly two decimal places.
Part 2: Compute Cafeteria Revenue
A cafeteria has a fixed seating capacity. You are given the capacity, an array fees where fees[i] is the fee customer i must pay the first time they successfully enter, and an array records of customer IDs processed in order. If a customer is currently outside, their record means they attempt to enter. They enter only if the cafeteria is not full. On the first successful entry only, add their fee to the total revenue. If they have entered successfully before, they do not pay again. If a customer is currently inside, their record means they leave. Failed entry attempts are ignored completely: the customer does not enter, is not queued, and must appear again later in records to try again. Return the total revenue collected.
Constraints
- 0 <= capacity <= len(fees) <= 2 * 10^5
- 0 <= fees[i] <= 10^9
- 0 <= len(records) <= 2 * 10^5
- Each records[k] is a valid customer ID in the range [0, len(fees) - 1]
Examples
Input: (2, [1, 2, 3], [0, 1, 0, 2, 1, 1, 1, 2])
Expected Output: 6
Explanation: Customers 0, 1, and 2 each pay once on their first successful entry, for a total of 1 + 2 + 3 = 6.
Input: (0, [5, 10], [0, 1, 0])
Expected Output: 0
Explanation: With zero capacity, every entry attempt fails.
Input: (3, [4, 8], [])
Expected Output: 0
Explanation: No records means no one enters and no revenue is collected.
Input: (1, [4], [0, 0, 0, 0])
Expected Output: 4
Explanation: Customer 0 enters, leaves, enters again, and leaves again. They pay only on the first successful entry.
Input: (1, [5, 7], [0, 1, 0, 1])
Expected Output: 12
Explanation: Customer 1 fails to enter while customer 0 is inside, but later enters successfully after 0 leaves. Total revenue is 5 + 7 = 12.
Hints
- Use one set to track who is currently inside and another set to track who has already paid.
- Process records strictly from left to right. A failed entry should do nothing except move on to the next record.