All puzzles › Computer Memory
Computer Memory Practice Problems
14 puzzles (5 easy, 7 medium, 2 hard), 4 free, in Python, JavaScript and C++.
How a computer actually holds your data. Two's complement, byte order, struct padding, caches, the stack, malloc, virtual memory and reference counting, each as a puzzle small enough to finish.
The puzzles, easiest first
- Free Bits in Every Pointer (Easy, free): Memory allocators hand out addresses that are multiples of 8, so the lowest three bits of every pointer are always zero.
- Flattening the Grid (Easy): Given a grid's rows and columns, a cell's row r and column c, and whether it's stored row-major, return how many spots from the start that cell is in memory.
- Page and Offset (Easy): Given a virtual address and a page size in bytes, return [page number, offset within the page].
- The Sign Bit (Easy): Given the raw bits of a signed integer, read as an unsigned number, and its width in bits, return the value it actually stores.
- Which End First? (Easy): Given the bytes of an integer in memory order and whether the machine is little endian, return the integer.
- Allocate by Moving One Number (Medium, free): The fastest allocator there is keeps a single number: where the free space begins.
- Neighbors on the Same Line (Medium, free): Each thread has its own counter, and they share nothing, yet the program crawls.
- Round Up, Then Split (Medium, free): A buddy allocator only deals in blocks whose sizes are powers of two.
- Hit or Miss (Medium): Given the byte addresses a program reads, in order, a cache's block size, and its number of lines, return how many reads hit in a direct-mapped cache that starts empty.
- Room for It (Medium): Given a memory bitmap where 0 is a free page and 1 is used, and a number k, return the starting index of the first run of k free pages in a row, or -1 if there isn't one.
- The Hidden Bytes (Medium): Given the sizes of a C struct's fields, in order, return the struct's total size in bytes.
- Too Deep (Medium): Given a list of calls, where a positive number is a call that adds a stack frame of that many bytes and 0 is a return from the latest call, plus a size limit, return the index of the first call that overflows the stack, or -1 if none does.
- First Hole That Fits (Hard): Given the sizes of free memory holes, in address order, and a list of requests, place each request in the FIRST hole big enough to hold it, and shrink that hole by the request.
- Held by Each Other (Hard): Given objects as an adjacency list of references and a list of roots, return how many objects are still allocated under reference counting.
Practice another pattern
Or browse by subject
Browse all 676 puzzles