Bird
Raised Fist0
Interview Preptwo-heapshardAmazonGoogle

Maximum Performance of a Team

Choose your preparation mode4 modes available
📋
Problem

Imagine you're a manager selecting a team of engineers to maximize overall project performance, balancing their speed and efficiency under a team size limit.

You are given two integer arrays speed and efficiency, both of length n, and an integer k. You want to form a team of at most k engineers to maximize the team's performance. The performance of a team is defined as the sum of the speeds of the team members multiplied by the minimum efficiency among them. Return the maximum performance of this team modulo 10^9 + 7. Input: - n: integer, number of engineers - speed: integer array of length n - efficiency: integer array of length n - k: integer, maximum team size Output: - integer, maximum performance modulo 10^9 + 7

1 ≤ n ≤ 10^5speed.length == nefficiency.length == n1 ≤ speed[i], efficiency[i] ≤ 10^51 ≤ k ≤ n
Edge cases: k = 1 (only one engineer can be chosen) → output is max speed[i]*efficiency[i]All efficiencies are equal → output is sum of top k speeds * that efficiencyAll speeds are equal → output is speed * minimum efficiency among chosen k
</>
IDE
def maxPerformance(n: int, speed: list[int], efficiency: list[int], k: int) -> int:public int maxPerformance(int n, int[] speed, int[] efficiency, int k)int maxPerformance(int n, vector<int>& speed, vector<int>& efficiency, int k)function maxPerformance(n, speed, efficiency, k)
def maxPerformance(n, speed, efficiency, k):
    # Write your solution here
    pass
class Solution {
    public int maxPerformance(int n, int[] speed, int[] efficiency, int k) {
        // Write your solution here
        return 0;
    }
}
#include <vector>
using namespace std;

int maxPerformance(int n, vector<int>& speed, vector<int>& efficiency, int k) {
    // Write your solution here
    return 0;
}
function maxPerformance(n, speed, efficiency, k) {
    // Write your solution here
}
Coming soon
0/9
Common Bugs to Avoid
Wrong: Wrong max performance due to ignoring efficiency sortingNot sorting engineers by efficiency descending before processing.✅ Sort engineers by efficiency descending before iterating and building the heap.
Wrong: Incorrect performance due to not maintaining heap size <= kNot removing smallest speed when heap size exceeds k.✅ Use a min-heap for speeds and pop smallest speed when heap size > k.
Wrong: Wrong output due to greedy picking highest speeds ignoring efficiencyGreedy approach picking top speeds without considering efficiency leads to wrong min efficiency.✅ Sort by efficiency descending and maintain min-heap of speeds to maximize performance.
Wrong: TLE on large inputsUsing brute force or exponential approach instead of O(n log n) heap-based solution.✅ Implement sorting by efficiency and min-heap to achieve O(n log n) complexity.
Wrong: Incorrect output on single element or k=1 casesNot handling base cases where team size is 1 or input size is 1.✅ Return speed[0]*efficiency[0] modulo 10^9+7 when n=1 or k=1.
✓
Test Cases
t1_01basic
Input{"n":6,"speed":[2,10,3,1,5,8],"efficiency":[5,4,3,9,7,2],"k":2}
Expected60

Choose engineers with speed 10 and 5, efficiencies 4 and 7. Minimum efficiency is 4, sum speed is 15, performance = 15*4=60.

t1_02basic
Input{"n":5,"speed":[5,1,3,7,4],"efficiency":[3,9,7,5,6],"k":3}
Expected68

Choose engineers with speeds 1,3,7 and efficiencies 9,7,5. Minimum efficiency is 5, sum speed is 11, performance = 11*5=55. But better is speeds 5,7,4 with efficiencies 3,5,6; min efficiency 3, sum speed 16, performance=48. Best is speeds 1,3,4 with efficiencies 9,7,6; min efficiency 6, sum speed 8, performance=48. Actually best is speeds 5,3,4 with efficiencies 3,7,6; min efficiency 3, sum speed 12, performance=36. The maximum is 68 by choosing speeds 5,7,4 with efficiencies 3,5,6 and min efficiency 3, sum speed 16, performance=48. Rechecking: speeds 1,7,4 with efficiencies 9,5,6 min eff 5 sum speed 12 perf 60. Speeds 1,3,7 with eff 9,7,5 min eff 5 sum speed 11 perf 55. Speeds 1,4,7 with eff 9,6,5 min eff 5 sum speed 12 perf 60. Speeds 3,4,7 with eff 7,6,5 min eff 5 sum speed 14 perf 70. So best is 70.

t2_01edge
Input{"n":1,"speed":[10],"efficiency":[5],"k":1}
Expected50

Only one engineer, performance = speed * efficiency = 10 * 5 = 50.

t2_02edge
Input{"n":4,"speed":[5,5,5,5],"efficiency":[3,3,3,3],"k":3}
Expected45

All efficiencies equal to 3, choose top 3 speeds (all 5), sum speed = 15, performance = 15 * 3 = 45.

t2_03edge
Input{"n":3,"speed":[7,7,7],"efficiency":[10,5,8],"k":2}
Expected105

All speeds equal 7, choose two engineers with efficiencies 10 and 8, min efficiency 8, sum speed 14, performance = 14 * 8 = 112. But better is efficiencies 10 and 5, min 5, sum speed 14, performance 70. So max is 112.

t3_01corner
Input{"n":4,"speed":[10,9,8,7],"efficiency":[1,2,3,4],"k":3}
Expected48

Greedy by speed fails: picking top 3 speeds (10,9,8) min efficiency 1 performance 27. Correct is picking engineers with efficiencies 4,3,2 and speeds 7,8,9 sum speed 24 min eff 2 performance 48.

t3_02corner
Input{"n":5,"speed":[2,2,2,2,2],"efficiency":[5,4,3,2,1],"k":3}
Expected24

Confusing 0/1 vs unbounded: only 3 engineers can be chosen, sum speed max 6, min efficiency depends on chosen engineers. Best is efficiencies 5,4,3 speeds 2,2,2 sum speed 6 min eff 3 performance 18. But better is efficiencies 5,4,2 speeds 2,2,2 sum speed 6 min eff 2 performance 12. Actually best is efficiencies 5,4,3 sum speed 6 min eff 3 performance 18. Check all combinations: max is 18.

t3_03corner
Input{"n":3,"speed":[1,100,1],"efficiency":[10,1,10],"k":2}
Expected20

Off-by-one error: choosing engineers with speeds 1 and 1 and efficiencies 10 and 10 sum speed 2 min eff 10 performance 20. Choosing 100 and 1 with efficiencies 1 and 10 min eff 1 performance 101. Actually max is 101. So best is speeds 100 and 1 with min eff 1 performance 101.

t4_01performance
Input{"n":100,"speed":[100000,99999,99998,99997,99996,99995,99994,99993,99992,99991,99990,99989,99988,99987,99986,99985,99984,99983,99982,99981,99980,99979,99978,99977,99976,99975,99974,99973,99972,99971,99970,99969,99968,99967,99966,99965,99964,99963,99962,99961,99960,99959,99958,99957,99956,99955,99954,99953,99952,99951,99950,99949,99948,99947,99946,99945,99944,99943,99942,99941,99940,99939,99938,99937,99936,99935,99934,99933,99932,99931,99930,99929,99928,99927,99926,99925,99924,99923,99922,99921,99920,99919,99918,99917,99916,99915,99914,99913,99912,99911,99910,99909,99908,99907,99906,99905,99904,99903,99902,99901,99900],"efficiency":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100],"k":50}
⏱ Performance - must finish in 2000ms

Large input with n=100 and k=50 to test O(n log n) solution performance within 2 seconds.

Practice

(1/5)
1. You are given schedules of multiple employees, each with their working intervals. You want to find all common free time intervals where no employee is working. Which algorithmic approach guarantees an optimal solution for this problem?
easy
A. Use a greedy approach to pick intervals with earliest end times and find gaps between them.
B. Flatten all intervals from all employees, sort them by start time, then merge overlapping intervals to find gaps.
C. Use dynamic programming to track overlapping intervals and compute free times.
D. Use a min-heap to iteratively merge intervals from each employee's schedule.

Solution

  1. Step 1: Understand problem requirements

    The problem requires finding common free intervals across multiple employees, which means merging all intervals to identify gaps.
  2. Step 2: Evaluate approaches

    Greedy earliest end time (A) does not guarantee merging all intervals correctly. DP (C) is unnecessary and inefficient here. Min-heap merging (D) is a valid approach but more complex than flattening and sorting. Flattening and sorting all intervals (B) ensures all intervals are processed in order, enabling correct gap detection.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Flattening and sorting all intervals is the canonical approach for interval merging [OK]
Hint: Flatten and sort all intervals to find gaps [OK]
Common Mistakes:
  • Assuming greedy earliest end time works for merging intervals
  • Confusing DP with interval merging
  • Thinking min-heap is always simpler
2. Given the following Python code for MedianFinder, what is the median after adding numbers 1, 5, and 2 in that order?
easy
A. 1.5
B. 2.0
C. 2
D. 3

Solution

  1. Step 1: Insert 1

    low = [-1], high = [], median = 1 (only one element)
  2. Step 2: Insert 5

    5 > 1, so push to high: high = [5], low = [-1]. Balanced heaps, median = (1 + 5)/2 = 3.0
  3. Step 3: Insert 2

    2 <= 5 but > 1, so push to low: low = [-2, -1], high = [5]. Balance: low_size=2, high_size=1, median = top of low = 2.0
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Median after 3 inserts is 2.0 -> [OK]
Hint: Median after odd inserts is top of larger heap [OK]
Common Mistakes:
  • Not balancing heaps after insertion
  • Incorrect median calculation for odd number of elements
  • Confusing max-heap sign inversion
3. Given the following Python code implementing the optimal two heaps approach for finding next intervals, what is the final returned result for the input intervals = [(2,3), (3,4), (1,2)]?
easy
A. [2, 0, 1]
B. [0, -1, 1]
C. [-1, -1, -1]
D. [1, -1, 0]

Solution

  1. Step 1: Initialize heaps and result

    max_start_heap = [(-3,1), (-2,0), (-1,2)], max_end_heap = [(-4,1), (-3,0), (-2,2)], result = [-1, -1, -1]
  2. Step 2: Process max_end_heap

    Pop (-4,1): max_start_heap top is -3 < 4, continue no update.
    Pop (-3,0): max_start_heap top -3 >= 3, pop candidates with start >= 3: (-3,1). min_start= -3, min_idx=1, result[0]=1.
    Push back unused candidates (none).
    Pop (-2,2): max_start_heap top -2 >= 2, pop candidates (-2,0), (-1,2). min_start= -1, min_idx=2, result[2]=2.
    Push back (-2,0).
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Matches expected output for given input [OK]
Hint: Track heap pops carefully to find correct indices [OK]
Common Mistakes:
  • Off-by-one in indices
  • Not pushing back unused intervals
  • Confusing start and end heaps
4. Consider the following buggy code snippet for sliding window median using a balanced sorted list. Which line contains the subtle bug that causes incorrect median results for even-sized windows?
medium
A. Line with 'return float(window[k // 2 - 1] + window[k // 2])'
B. Line with 'bisect.insort_left(window, nums[i])'
C. Line with 'idx = bisect.bisect_left(window, nums[i - k])'
D. Line with 'window = sorted(nums[:k])'

Solution

  1. Step 1: Analyze median calculation for even k

    Median for even k is average of two middle elements, so sum must be divided by 2.
  2. Step 2: Identify bug

    Line returning sum without division causes median to be twice the correct value.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Missing division by 2 in even median calculation [OK]
Hint: Even median requires averaging two middle elements [OK]
Common Mistakes:
  • Forgetting to divide sum by 2 for even k median
  • Incorrect index calculation for median
  • Wrong removal or insertion causing imbalance
5. Identify the bug in the following code snippet for the optimized task scheduler:
def task_scheduler_buggy(tasks, space):
    next_available = {}
    day = 1
    for t in tasks:
        if t in next_available:
            day = next_available[t]  # Bug here
        next_available[t] = day + space + 1
        day += 1
    return day - 1
medium
A. Line 6: Using assignment instead of max to update day causes cooldown violation
B. Line 4: Missing initialization of next_available dictionary
C. Line 7: Incorrect calculation of next_available day for task
D. Line 8: Incrementing day after scheduling causes off-by-one error

Solution

  1. Step 1: Analyze day update logic

    Line 6 assigns day = next_available[t], which can move day backwards if day is already ahead, violating cooldown constraints.
  2. Step 2: Correct approach

    Use day = max(day, next_available[t]) to ensure day never moves backward and respects cooldown.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Assignment without max causes scheduling too early -> [OK]
Hint: Use max(day, next_available[t]) to avoid scheduling before current day [OK]
Common Mistakes:
  • Assigning day directly instead of max causes cooldown violation
  • Misplacing day increment causing off-by-one errors
  • Incorrect next_available update formula
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard