Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Build Mobile API Request Cache

Volvo CarsEasyPython00:00
Volvo Cars
Your interviewer · Mobile Engineer
In session
Interviewer

Welcome to the Python screen for the Mobile Engineer role at Volvo Cars.

The question is on your right: Build Mobile API Request Cache. Read through the requirements first.

Would you like to talk through your approach, or are you ready to start coding?

Talking through your plan before coding is graded well.

Problem

A mobile app at PulseFit calls several REST endpoints repeatedly. To reduce unnecessary network requests, implement an in-memory cache that stores API responses by endpoint key and expires entries after a fixed time-to-live.

Write a function that processes a sequence of cache operations and returns the result of each get operation.

Formal Specification

Implement process_api_cache(operations, ttl) where:

  • operations is a list of operations.
  • Each operation is one of:
    • ["set", key, value, timestamp]
    • ["get", key, timestamp]
    • ["invalidate", key, timestamp]
  • key and value are strings.
  • timestamp is a non-negative integer.
  • ttl is a positive integer.

For a get at time t, return the cached value only if the key exists and t - set_timestamp < ttl. Otherwise return "MISS". An invalidate removes the key immediately if present. Return a list containing the outputs of all get operations in order.

Constraints

  • 1 <= len(operations) <= 10^5
  • 1 <= ttl <= 10^9
  • Each key and value is a non-empty string
  • Timestamps are non-negative integers
  • Operations are processed in the given order

Function Signature

def process_api_cache(operations, ttl):
Your solutionPython 3
Run as often as you like, then submit when the output looks right.
Run your code to see test output