The Number of Seniors and Juniors to Join the Company II - Problem
A company wants to hire new employees with a budget of $70000.
Hiring criteria:
- Keep hiring the senior with the smallest salary until you cannot hire any more seniors
- Use the remaining budget to hire the junior with the smallest salary
- Keep hiring the junior with the smallest salary until you cannot hire any more juniors
Write a solution to find the employee_id of seniors and juniors hired under the mentioned criteria.
Table Schema
Candidates
| Column Name | Type | Description |
|---|---|---|
employee_id
PK
|
int | Unique identifier for each candidate |
experience
|
enum | Experience level: 'Senior' or 'Junior' |
salary
|
int | Monthly salary (guaranteed to be unique) |
Primary Key: employee_id
Note: Each candidate has a unique salary amount
Input & Output
Example 1 — Basic Hiring Scenario
Input Table:
| employee_id | experience | salary |
|---|---|---|
| 1 | Senior | 10000 |
| 2 | Junior | 5000 |
| 3 | Senior | 50000 |
| 4 | Junior | 20000 |
Output:
| employee_id |
|---|
| 1 |
| 3 |
| 2 |
💡 Note:
First, hire seniors by salary order: employee 1 ($10,000) and employee 3 ($50,000) totaling $60,000. With $10,000 remaining, hire junior employee 2 ($5,000). Junior employee 4 ($20,000) exceeds remaining budget.
Example 2 — Budget Exhausted by Seniors
Input Table:
| employee_id | experience | salary |
|---|---|---|
| 1 | Senior | 70000 |
| 2 | Junior | 5000 |
Output:
| employee_id |
|---|
| 1 |
💡 Note:
Senior employee 1 uses the entire $70,000 budget, leaving no funds for junior candidates. Only employee 1 is hired.
Example 3 — No Seniors Available
Input Table:
| employee_id | experience | salary |
|---|---|---|
| 1 | Junior | 10000 |
| 2 | Junior | 30000 |
| 3 | Junior | 40000 |
Output:
| employee_id |
|---|
| 1 |
| 2 |
💡 Note:
No seniors available, so entire $70,000 budget goes to juniors. Hire junior 1 ($10,000) and junior 2 ($30,000) totaling $40,000. Junior 3 ($40,000) would exceed the remaining $30,000.
Constraints
-
1 ≤ employee_id ≤ 1000 -
experienceis either'Senior'or'Junior' -
1 ≤ salary ≤ 70000 - The salary of each candidate is guaranteed to be unique
Visualization
Tap to expand
Understanding the Visualization
1
Input
Candidates with experience levels and salaries
2
Process
Running totals with budget constraints
3
Output
Selected employee IDs within budget
Key Takeaway
🎯 Key Insight: Use window functions with running totals to efficiently track budget consumption across different candidate groups
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code