Interview questions · Snapchat
Implement LRU cache and prime products array
This question evaluates proficiency in implementing efficient mutable data structures and in enumerating combinatorial results, specifically testing understanding of LRU cache eviction behavior and generation of all non-empty subset products from a set of primes.
You are given two separate coding tasks.
Task 1: Implement an LRU cache
Implement an in-memory cache with a fixed capacity that evicts the least recently used (LRU) key when the capacity is exceeded.
Implement the following operations:
get(key): return the value associated withkeyif present; otherwise return -1.put(key, value): insert or update the value forkey. If the cache exceeds its capacity after this operation, evict the least recently used key.
Both operations must run in average O(1) time.
Assume keys and values are integers. The cache capacity is a positive integer given at construction time.
Task 2: Generate products from an array of primes
You are given an array primes of distinct prime integers, length n where 1 <= n <= 15 and each value is in the range [2, 10^6].
Return a sorted list (ascending order) containing all unique products that can be formed by taking the product of any non-empty subset of primes. Each prime can be used at most once in a product (that is, you are considering subsets, not multisets).
Example:
- Input:
primes = [2, 3, 11] - All non-empty subsets and their products:
[2]-> 2[3]-> 3[11]-> 11[2, 3]-> 6[2, 11]-> 22[3, 11]-> 33[2, 3, 11]-> 66
- Output:
[2, 3, 6, 11, 22, 33, 66]
Community answers
Nothing here yet.
Join in on CSWork →