Implement LRU cache and prime products array

Quick Overview

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.

Implement LRU cache and prime products array

Company: Snapchat

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

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 with `key` if present; otherwise return -1. - `put(key, value)`: insert or update the value for `key`. 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]`

Quick Answer: 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.

|Home/Coding & Algorithms/Snapchat
Snapchat logo
Snapchat
Oct 28, 2025, 12:00 AM
hardSoftware EngineerOnsiteCoding & Algorithms
12
0

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 with key if present; otherwise return -1.
  • put(key, value) : insert or update the value for key . 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]

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...