Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Data Structures Coding Challenge

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Data Structures Coding Challenge. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

University of Michigan Canvas services may cache recently accessed course data, but the cache must evict the least recently used item when it reaches capacity. Implement an LRU cache that supports get and put operations efficiently.

Formal Specification

Write lru_cache_operations(capacity, operations), where capacity is an integer and operations is a list of operations. Each operation is one of:

  • ['get', key]: Return the value for key, or -1 if the key is absent. A successful get makes the key most recently used.
  • ['put', key, value]: Insert or update the key. The operation produces no output. An updated key becomes most recently used.

Return a list containing the results of all get operations, in order. Use a hash map plus a doubly linked list so both operations run in constant time.

Constraints

  • 1 <= capacity <= 10^5
  • 1 <= len(operations) <= 2 * 10^5
  • Keys and values are integers
  • Each operation is valid and has either two or three elements

Function Signature

def lru_cache_operations(capacity, operations):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output