Simulate a browser cookie jar with set, get, delete and getAll operations

Quick Overview

Simulate a browser cookie jar that processes set, get, delete and getAll operations in order and returns the result of every read. It tests hash map use, handling of missing keys, and producing a deterministic, sorted key=value serialization.

Simulate a browser cookie jar with set, get, delete and getAll operations

Company: Splunk

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Simulate the cookie storage of a browser. You are given a list of operations to run against a cookie jar. Each operation is one of the following: - `["set", key, value]`: store a cookie with the given `key` and `value`. If the key already exists, overwrite its value. - `["set", key, value, days]`: store a cookie with the given `key`, `value` and an expiration in `days`. In this simulation the expiration is stored but does not affect retrieval. - `["get", key]`: return the value stored for `key`, or the empty string `""` if the key does not exist. - `["delete", key]`: remove the cookie with the given `key`. If the key does not exist, do nothing. - `["getAll"]`: return all stored cookies as one string of `key=value` pairs, sorted by key and joined by `"; "`. If the jar is empty, return `""`. Process the operations in order and return a list with the result of every `get` and `getAll` operation, in the order they were encountered. ### Function Signature ```python def cookie_operations(operations: list[list[str]]) -> list[str]: ``` ### Rules - Every element of an operation is a string, including `days` (for example `"7"`). - `set` and `delete` produce no output. Only `get` and `getAll` add an entry to the result list. - Keys are case-sensitive: `"id"` and `"ID"` are different cookies. - `getAll` sorts keys by character code, the order Python's default string comparison uses. Uppercase letters come before lowercase letters, and `-` comes before digits. - There are no spaces around `=` and no separator after the last pair. ### Constraints - `1 <= len(operations) <= 1000` - `1 <= len(key) <= 100` - `1 <= len(value) <= 1000` - Keys and values contain only letters, digits, hyphens and underscores. - At least one `get` or `getAll` operation is present. ### Examples ```text Input: operations = [["set","username","alice"],["set","theme","dark"],["get","username"],["getAll"]] Output: ["alice","theme=dark; username=alice"] ``` The `get` returns `"alice"`. `getAll` lists both cookies sorted by key, and `"theme"` sorts before `"username"`. ```text Input: operations = [["set","token","abc123"],["get","token"],["delete","token"],["get","token"]] Output: ["abc123",""] ``` The first `get` finds the cookie. After the delete the key is gone, so the second `get` returns `""`. ```text Input: operations = [["set","id","u1","30"],["set","Session","s9"],["set","id","u2"],["delete","zzz"],["getAll"],["delete","id"],["delete","Session"],["getAll"]] Output: ["Session=s9; id=u2",""] ``` The third operation overwrites `id` with `u2`, and deleting the missing key `zzz` does nothing. `"Session"` sorts first because `S` (code 83) is smaller than `i` (code 105), and the last `getAll` runs on an empty jar.

Overview: Simulate a browser cookie jar that processes set, get, delete and getAll operations in order and returns the result of every read. It tests hash map use, handling of missing keys, and producing a deterministic, sorted key=value serialization.

Splunk logo
Splunk
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Simulate the cookie storage of a browser. You are given a list of operations to run against a cookie jar. Each operation is one of the following:

  • ["set", key, value] : store a cookie with the given key and value . If the key already exists, overwrite its value.
  • ["set", key, value, days] : store a cookie with the given key , value and an expiration in days . In this simulation the expiration is stored but does not affect retrieval.
  • ["get", key] : return the value stored for key , or the empty string "" if the key does not exist.
  • ["delete", key] : remove the cookie with the given key . If the key does not exist, do nothing.
  • ["getAll"] : return all stored cookies as one string of key=value pairs, sorted by key and joined by "; " . If the jar is empty, return "" .

Process the operations in order and return a list with the result of every get and getAll operation, in the order they were encountered.

Function Signature

def cookie_operations(operations: list[list[str]]) -> list[str]:

Rules

  • Every element of an operation is a string, including days (for example "7" ).
  • set and delete produce no output. Only get and getAll add an entry to the result list.
  • Keys are case-sensitive: "id" and "ID" are different cookies.
  • getAll sorts keys by character code, the order Python's default string comparison uses. Uppercase letters come before lowercase letters, and - comes before digits.
  • There are no spaces around = and no separator after the last pair.

Constraints

  • 1 <= len(operations) <= 1000
  • 1 <= len(key) <= 100
  • 1 <= len(value) <= 1000
  • Keys and values contain only letters, digits, hyphens and underscores.
  • At least one get or getAll operation is present.

Examples

Input: operations = [["set","username","alice"],["set","theme","dark"],["get","username"],["getAll"]]
Output: ["alice","theme=dark; username=alice"]

The get returns "alice". getAll lists both cookies sorted by key, and "theme" sorts before "username".

Input: operations = [["set","token","abc123"],["get","token"],["delete","token"],["get","token"]]
Output: ["abc123",""]

The first get finds the cookie. After the delete the key is gone, so the second get returns "".

Input: operations = [["set","id","u1","30"],["set","Session","s9"],["set","id","u2"],["delete","zzz"],["getAll"],["delete","id"],["delete","Session"],["getAll"]]
Output: ["Session=s9; id=u2",""]

The third operation overwrites id with u2, and deleting the missing key zzz does nothing. "Session" sorts first because S (code 83) is smaller than i (code 105), and the last getAll runs on an empty jar.