Step 1: Calculate total rating sum before new review
Total sum = current_avg * num_reviews = 4.0 * 5 = 20
Step 2: Add new rating and compute new average
New sum = 20 + 5 = 25 New average = 25 / (5 + 1) = 25 / 6 ≈ 4.1667
Final Answer:
4.17 -> Option A
Quick Check:
Average update formula ≈ 4.17 [OK]
Hint: Multiply avg by count, add new, divide by count+1 [OK]
Common Mistakes:
Forgetting to add new rating to total sum
Dividing by old count instead of count+1
Rounding too early causing wrong average
4. A rating system stores average rating and count per product. After deleting a review, the average becomes incorrect. What is the likely cause?
medium
A. Recalculating average using sum of all reviews
B. Using integer division instead of float division
C. Not updating the count of reviews after deletion
D. Storing reviews in a hash map
Solution
Step 1: Understand average calculation
Average = sum of ratings / count of reviews. Both must be accurate.
Step 2: Identify deletion impact
If count is not decreased after deleting a review, average calculation divides by wrong count.
Final Answer:
Not updating the count of reviews after deletion -> Option C
Quick Check:
Count mismatch causes wrong average [OK]
Hint: Always update count when reviews change [OK]
Common Mistakes:
Ignoring count update after deletion
Assuming recalculation always fixes average
Confusing data structure choice with calculation error
5. You want to design a scalable rating and review system for millions of products and users. Which approach best balances fast average rating queries and frequent review updates?
hard
A. Store all reviews and compute average on each query
B. Use a single database table without indexes
C. Cache only the latest review per product
D. Maintain precomputed average and count, update incrementally on review changes
Solution
Step 1: Consider query and update load
Millions of products and users mean many queries and updates.
Step 2: Choose efficient strategy
Precomputing average and count and updating them incrementally avoids scanning all reviews each time.
Step 3: Evaluate other options
Computing average on each query is slow; no indexes cause slow lookups; caching only latest review misses full rating info.
Final Answer:
Maintain precomputed average and count, update incrementally on review changes -> Option D
Quick Check:
Precompute + incremental update = scalable [OK]
Hint: Precompute averages, update on changes for scale [OK]