Bird
Raised Fist0
Interview Prepgreedy-algorithmseasyAmazonGoogle

Maximum Units on a Truck

Choose your preparation mode4 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Steps
setup

Start with unsorted boxTypes

The algorithm begins with the original boxTypes array and the truckSize available.

💡 Seeing the initial input helps understand the starting point before sorting.
Line:boxTypes = [[1,3],[2,2],[3,1]] truckSize = 4
💡 Initial data setup is crucial before any processing.
📊
Maximum Units on a Truck - Watch the Algorithm Execute, Step by Step
Watching the algorithm step through sorting and greedy selection reveals how early stopping and inline calculations optimize the solution efficiently.
Step 1/17
·Active fillAnswer cell
setup
[1,3]
0
[2,2]
1
[3,1]
2
Result: 0
sort
[1,3]
0
[2,2]
1
[3,1]
2
Result: 0
initialize
[1,3]
0
[2,2]
1
[3,1]
2
Result: 0
compare
i
[1,3]
0
[2,2]
1
[3,1]
2
Result: 0
move_right
i
[1,3]
0
[2,2]
1
[3,1]
2
Result: 0
record
i
[1,3]
0
[2,2]
1
[3,1]
2
Result: 3
move_left
i
[1,3]
0
[2,2]
1
[3,1]
2
Result: 3
compare
[1,3]
0
i
[2,2]
1
[3,1]
2
Result: 3
move_right
[1,3]
0
i
[2,2]
1
[3,1]
2
Result: 3
record
[1,3]
0
i
[2,2]
1
[3,1]
2
Result: 7
move_left
[1,3]
0
i
[2,2]
1
[3,1]
2
Result: 7
compare
[1,3]
0
[2,2]
1
i
[3,1]
2
Result: 7
move_right
[1,3]
0
[2,2]
1
i
[3,1]
2
Result: 7
record
[1,3]
0
[2,2]
1
i
[3,1]
2
Result: 8
move_left
[1,3]
0
[2,2]
1
i
[3,1]
2
Result: 8
prune
[1,3]
0
[2,2]
1
i
[3,1]
2
Result: 8
return
[1,3]
0
[2,2]
1
[3,1]
2
Result: 8

Key Takeaways

Sorting box types by units per box descending is the foundation of the greedy approach.

This insight is hard to see from code alone because sorting is a separate step that enables the greedy selection order.

The algorithm picks as many boxes as possible from the highest unit box type before moving on.

Visualizing the pointer moving and capacity shrinking clarifies how the greedy choice is applied stepwise.

Early stopping when truckSize reaches zero avoids unnecessary processing and improves efficiency.

Seeing the break condition in action helps understand the optimization beyond the basic greedy logic.

Practice

(1/5)
1. You are given a list of sticks with different lengths. You want to connect all sticks into one by repeatedly merging any two sticks, paying a cost equal to the sum of their lengths each time. Which algorithmic approach guarantees the minimum total cost to connect all sticks?
easy
A. Dynamic Programming that tries all possible merge sequences to find the minimum cost
B. Greedy algorithm using a min-heap to always merge the two shortest sticks first
C. Sorting the sticks once and merging them in sorted order from smallest to largest
D. Greedy algorithm that merges the two longest sticks first to reduce future costs

Solution

  1. Step 1: Understand the problem goal

    The goal is to minimize the total cost of merging sticks, where each merge cost equals the sum of the two sticks merged.
  2. Step 2: Identify the optimal strategy

    Merging the two shortest sticks first at each step minimizes incremental cost and leads to the global minimum total cost. This is efficiently done using a min-heap.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Min-heap merges shortest sticks first -> minimal total cost [OK]
Hint: Always merge shortest sticks first for minimal cost [OK]
Common Mistakes:
  • Merging longest sticks first thinking it reduces future costs
  • Sorting once and merging in order without reordering after merges
  • Assuming brute force is needed for minimal cost
2. Consider the following Python code snippet implementing the optimal solution to remove k digits from a number string to get the smallest number. What is the output of removeKdigits("1432", 2)?
def removeKdigits(num: str, k: int) -> str:
    builder = []
    for digit in num:
        while k > 0 and builder and builder[-1] > digit:
            builder.pop()
            k -= 1
        builder.append(digit)
    while k > 0:
        builder.pop()
        k -= 1
    result = ''.join(builder).lstrip('0')
    return result if result else '0'
easy
A. "14"
B. "13"
C. "12"
D. "32"

Solution

  1. Step 1: Trace builder and k during iteration

    Start with builder = [], k=2 - digit='1': builder=['1'], k=2 - digit='4': '4' > '1', append: builder=['1','4'], k=2 - digit='3': '3' < '4', pop '4', k=1; append '3': builder=['1','3'] - digit='2': '2' < '3', pop '3', k=0; append '2': builder=['1','2']
  2. Step 2: Remove remaining k and finalize

    k=0, no more pops. Result = '12' after stripping leading zeros.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Output matches expected smallest number after removing 2 digits [OK]
Hint: Pop larger digits when smaller digit found until k=0 [OK]
Common Mistakes:
  • Not popping enough digits when smaller digit appears
  • Removing digits from front only
  • Forgetting to strip leading zeros
3. The following code attempts to implement the peak-valley approach but contains a subtle bug. Identify the line causing incorrect profit calculation.
def maxProfit(prices):
    i = 0
    profit = 0
    n = len(prices)
    while i < n - 1:
        while i < n - 1 and prices[i] > prices[i + 1]:
            i += 1
        valley = prices[i]
        while i < n - 1 and prices[i] < prices[i + 1]:
            i += 1
        peak = prices[i]
        profit += peak - valley
    return profit
medium
A. Line with 'while i < n - 1 and prices[i] < prices[i + 1]:'
B. Line with 'valley = prices[i]'
C. Line with 'while i < n - 1 and prices[i] > prices[i + 1]:'
D. Line with 'profit += peak - valley'

Solution

  1. Step 1: Compare with correct condition

    The correct condition should be prices[i] >= prices[i + 1] to skip equal or descending prices.
  2. Step 2: Identify bug impact

    Using strict '>' misses equal prices, causing incorrect valley selection and possibly negative profit.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Changing '>' to '>=' fixes edge cases with flat prices [OK]
Hint: Check comparison operators in loops for off-by-one errors [OK]
Common Mistakes:
  • Using strict inequalities causing missed equal prices
  • Adding negative differences to profit
  • Off-by-one errors causing index out of range
4. Consider the following buggy code for the Gas Station problem. Which line contains the subtle bug that can cause incorrect results?
def canCompleteCircuit(gas, cost):
    n = len(gas)
    net = [gas[i] - cost[i] for i in range(n)]
    # Bug: missing total gas check
    prefix = [0] * (2 * n + 1)
    for i in range(2 * n):
        prefix[i+1] = prefix[i] + net[i % n]
    for i in range(n):
        if prefix[i+n] - prefix[i] >= 0:
            return i
    return -1
medium
A. Line 3: net array computation
B. Line 4: missing total gas vs total cost check
C. Line 6: prefix sums computation loop
D. Line 8: checking prefix sums for valid start

Solution

  1. Step 1: Identify missing total gas check

    The code does not check if sum(net) < 0 before proceeding, which can cause incorrect start index or false positives.
  2. Step 2: Verify other lines

    Net array, prefix sums, and prefix difference checks are correct and standard.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Missing total gas check leads to incorrect results [OK]
Hint: Always check total gas >= total cost before searching start [OK]
Common Mistakes:
  • Forgetting total gas check
  • Misusing modulo in prefix sums
  • Resetting start without resetting tank
5. What is the time complexity of the optimal greedy solution for Two City Scheduling that sorts by cost difference and assigns people in a single pass?
medium
A. O(n^2) because of nested loops to assign people
B. O(n log n) due to sorting the list of 2n people
C. O(n) since assignment is done in one pass after sorting
D. O(n log n) including sorting and constant time assignment

Solution

  1. Step 1: Identify sorting cost

    Sorting 2n people by cost difference takes O(n log n) time.
  2. Step 2: Identify assignment cost

    Assigning people in a single pass is O(n).
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Sorting dominates, total complexity is O(n log n) [OK]
Hint: Sorting dominates time complexity [OK]
Common Mistakes:
  • Assuming assignment is nested loop causing O(n^2)
  • Ignoring sorting cost and claiming O(n)
  • Confusing space complexity with time complexity