Count Apples and Oranges - Problem

Imagine you're working at a fruit distribution warehouse where fruits are stored in boxes, and some boxes contain chests for additional storage capacity.

You have two tables:

  • Boxes table: Contains information about each box including its ID, optional chest ID, and direct fruit counts
  • Chests table: Contains information about chests and their fruit counts

Your task is to calculate the total number of apples and oranges across all boxes. Remember: if a box contains a chest, you must add both the box's fruits and the chest's fruits to get the complete count.

Goal: Return a single row with the total apple count and total orange count from all boxes (including their chests if any).

Input & Output

example_1.sql โ€” Basic Example
$ Input: Boxes: [(1,100,1,4), (2,null,2,3)] Chests: [(100,5,6)]
โ€บ Output: apple_count: 8, orange_count: 13
๐Ÿ’ก Note: Box 1 has chest 100: (1+5=6 apples, 4+6=10 oranges). Box 2 has no chest: (2 apples, 3 oranges). Total: 6+2=8 apples, 10+3=13 oranges
example_2.sql โ€” No Chests
$ Input: Boxes: [(1,null,3,2), (2,null,1,4)] Chests: []
โ€บ Output: apple_count: 4, orange_count: 6
๐Ÿ’ก Note: No boxes have chests, so we just sum the box contents: 3+1=4 apples, 2+4=6 oranges
example_3.sql โ€” All Have Chests
$ Input: Boxes: [(1,200,0,1), (2,201,1,0)] Chests: [(200,10,5), (201,2,3)]
โ€บ Output: apple_count: 13, orange_count: 9
๐Ÿ’ก Note: Box 1+chest 200: 0+10=10 apples, 1+5=6 oranges. Box 2+chest 201: 1+2=3 apples, 0+3=3 oranges. Total: 10+3=13 apples, 6+3=9 oranges

Visualization

Tap to expand
๐Ÿ“ฆ Warehouse BoxesBox 1๐ŸŽ2 ๐ŸŠ3Box 2๐ŸŽ1 ๐ŸŠ2Box 3๐ŸŽ0 ๐ŸŠ1ChestChestBox 2 has no chest๐ŸŽฏ LEFT JOIN ResultBox 1: 2+5 = 7 ๐ŸŽ, 3+4 = 7 ๐ŸŠBox 2: 1+0 = 1 ๐ŸŽ, 2+0 = 2 ๐ŸŠBox 3: 0+2 = 2 ๐ŸŽ, 1+3 = 4 ๐ŸŠ๐Ÿ“Š Final Count๐ŸŽ Apples: 10๐ŸŠ Oranges: 13๐ŸŽ5 ๐ŸŠ4๐ŸŽ2 ๐ŸŠ3
Understanding the Visualization
1
Identify Data Structure
Boxes table with optional chest references, Chests table with fruit counts
2
LEFT JOIN Operation
Connect each box with its chest (if any), keeping all boxes
3
Handle Missing Chests
Use COALESCE to treat NULL chest values as 0
4
Sum All Fruits
Add box fruits + chest fruits for each row, then sum totals
Key Takeaway
๐ŸŽฏ Key Insight: LEFT JOIN preserves all boxes while optionally including chest data, and COALESCE handles missing chests gracefully by treating them as 0 fruit contributions.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log m)

JOIN operation with potential index lookup where n is boxes and m is chests

n
2n
โšก Linearithmic
Space Complexity
O(1)

Only storing final aggregated results

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค box_id โ‰ค 103
  • 1 โ‰ค chest_id โ‰ค 103
  • 0 โ‰ค apple_count, orange_count โ‰ค 104
  • Each box can contain at most one chest
  • chest_id in Boxes can be NULL (indicating no chest)
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
23.4K Views
Medium Frequency
~15 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