Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Create a function to implement a basic caching mechanism for expensive calculations.

Hard

Problem Statement

Create a function to implement a basic caching mechanism for expensive calculations.

Sample Input

Function to be cached: expensiveCalculation
Cache limit: 5

Sample Output

100

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
// Implement a caching mechanism with a specified limit function cacheFunction(func, limit) { // Initialize a cache object and a queue for tracking entries // Return a function that checks cache before performing calculation } // Example function to cache function expensiveCalculation(num) { // Perform an intensive calculation and return the result } const cachedExpensiveCalculation = cacheFunction(expensiveCalculation, 5); console.log(cachedExpensiveCalculation(10));
Log in or sign up to run or submit your code.Log inSign up
Input
Function to be cached: expensiveCalculation
Cache limit: 5
Expected Output
100