Skip to main content

Practice ยท 4 of 4

Debug: The Loader That Calls Too Much

CHALLENGE
Difficulty: intermediate+25 XP

A junior "fixed" the N+1 by caching โ€” but their CachedLoader still hits the store once per customer AND runs a pointless "warm-up" second pass. The counting fake exposes both: Calls grows and LastIds fills with single-id batches. Write a correct Solution.Load(ILookup store, int[] ids) that - performs exactly one FetchMany per invocation (dedup ids), - returns names in input order (with repeats), - and makes no store call at all for empty input. ``csharp public static class Solution { public static string[] Load(ILookup store, int[] ids); } // top level: public interface ILookup { string[] FetchMany(int[] ids); int Calls { get; } } ` Provided for reference โ€” the broken version you are correcting: `csharp public sealed class CachedLoader : ILoader // BROKEN: fetches per id, twice { public string[] Load(ILookup store, int[] ids) { var outp = new List<string>(); foreach (var id in ids) { var n = store.FetchMany(new[]{id}); outp.Add(n[0]); } foreach (var id in ids) { store.FetchMany(new[]{id}); } // "warm the cache" return outp.ToArray(); } } ``

Back to lesson: Practice: Architecture Practice: Seams, Batches, Boundaries