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
  • experience is either 'Senior' or 'Junior'
  • 1 ≤ salary ≤ 70000
  • The salary of each candidate is guaranteed to be unique

Visualization

Tap to expand
Seniors & Juniors Hiring Problem INPUT Budget: $70,000 Candidates Table emp_id experience salary 1 Senior $10,000 2 Senior $20,000 3 Senior $30,000 4 Junior $10,000 5 Junior $15,000 6 Junior $20,000 Senior Junior Goal: Maximize hires Seniors first, then Juniors ALGORITHM STEPS 1 Sort by Salary Order each group by salary ascending 2 Hire Seniors First Use running sum to hire cheapest seniors Senior 1: $10K [OK] Total: $10K Senior 2: $20K [OK] Total: $30K Senior 3: $30K [OK] Total: $60K Remaining: $10K 3 Hire Juniors Use remaining budget for juniors Junior 4: $10K [OK] Total: $70K Junior 5: $15K [X] Over budget 4 Return Results Collect hired emp_ids FINAL RESULT Hired Seniors ID: 1 ID: 2 ID: 3 Cost: $60,000 Hired Juniors ID: 4 Cost: $10,000 Summary Total Hired: 4 employees Total Spent: $70,000 Budget fully utilized! Key Insight: Use Window Functions (SUM OVER) to compute running totals of salaries. Filter candidates whose cumulative cost fits within budget. Greedy approach: Hiring cheapest seniors first maximizes remaining budget for juniors, ensuring optimal total hires. TutorialsPoint - The Number of Seniors and Juniors to Join the Company II | Optimal Solution
Asked in
Amazon 15 Microsoft 12
32.0K Views
Medium Frequency
~20 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen