Ctrl K
Frontend · Senior · Coding

Write a memoizeAsync function. It takes an async function of a string key and a cache time to live in milliseconds, and returns a wrapper: a repeated call with the same key doesn't go to the network, concurrent calls share a single request, and errors aren't cached.

~15 min by voice

How to approach it

This checks whether you understand JS's async model: that you need to cache the promise, not the result, so concurrent calls share one request. Interviewers also look at how you handle races around errors and TTL and how you manage the cache's lifecycle (size, invalidation).

Have you seen this question in a real interview?
Answers0
Avg. score—
Strong—
Practiced0
Topics
Similar questionsПеред базой стоит кеш в Redis с карточками товаров, TTL — пять минут. Расскажите, как бы вы организовали чтение и обновление, как кеш может разойтись с базой, и что произойдёт, когда истечёт TTL у очень популярного ключа. Mid-levelWrite a retry function. It takes an async function and options: the number of attempts, a base delay, and an AbortSignal. It calls the function and, on error, retries it with an exponentially growing delay. It returns a promise with the first successful result or the last error. SeniorThere are price providers, and each one has a price method that takes a SKU and asynchronously returns a CompletableFuture with a price. Write a method bestPrice that takes a SKU and a list of providers, queries all of them in parallel, and returns a CompletableFuture with the lowest price: providers that fail or don't respond within two seconds are skipped, and if nobody returned a price, the result completes with an error. SeniorYou added a cache in front of the database for product cards, and users started complaining that they see outdated prices. How should you set up caching to avoid this? Mid-levelDesign a URL shortener: a user submits a long URL and gets a short one, and following the short link redirects to the original. Mid-level

Answer by voice

The interviewer will ask this question and listen. Think out loud and ask for a hint if you need one. When you're done, pause or tap “That's my answer”. In about a minute, your score and the points you covered show up here.

Sign in to answer

Best with headphones · ~15 min