FastPrepMemory Allocator
Problems/Memory Allocator

Memory Allocator

TikTok logoTikTok● MediumFULLTIMEOA

Updated Sep 2026

QuestionFunctionExamples

Problem statement

Given a memory array consisting of 0s and 1s (0 = free, 1 = occupied). Memory is aligned by 8.

Support the following two operations, provided as a list of strings:

  • alloc x — Find the leftmost contiguous block of x free units whose starting index is divisible by 8. Allocate it (mark as occupied and assign a unique ID). Return the starting index, or -1 if not found.
  • erase id — Scan the entire array, reset all positions with the given ID to 0, and return the number of cleared units.

IDs are assigned incrementally starting from 1. Given the initial memory array and a list of operations, return an array of integer results — one per operation.

Note: The starting index of an allocated block must be divisible by 8. The block itself may span beyond the next multiple of 8.

Function

memoryAllocator(memory: int[], operations: String[]) → int[]

Examples

Example 1

memory = [0,0,0,0,0,0,0,0]operations = ["alloc 3","erase 1"]return = [0, 3]

alloc 3: starting index 0 is divisible by 8, positions 0–2 are all free → allocate with ID=1, return 0. erase 1: positions 0–2 have ID=1 → clear them and return 3.

Example 2

memory = [1,1,1,1,1,1,1,1]operations = ["alloc 1"]return = [-1]

alloc 1: the only valid starting index is 0, but position 0 is occupied → return -1.

More TikTok interview questions

  • Center of the Largest Perfect X-ShapeOA · Seen Nov 2026
  • Count Subarrays Matching a Comparison PatternOA · Seen Nov 2026
  • Count Suffix Word PairsOA · Seen Nov 2026
  • Minimum Cost of Every K-Capable Model SetOA · Seen Nov 2026
  • Longest Substring Without Repeating CharactersPHONE SCREEN · Seen Oct 2026
  • Next Greater Indices and Maximum GapPHONE SCREEN · Seen Oct 2026
  • Count Subarrays with at Least K Equal-Fruit PairsOA · Seen Oct 2026
  • Shell Command Counts with History ReferencesOA · Seen Oct 2026
  • Product Minus Sum of DigitsOA · Seen Aug 2026
  • Count 2x2 Submatrices by Black CellsOA · Seen Aug 2026

Similar questions at other companies

  • Bank Accounts with Balance HistoryMeta · OAMeta interview questions
  • Process Banking OperationsVercel · OA
  • Adding Stack 2.0IMC Trading · OAIMC Trading interview questions
  • Integer Map Using a VectorAdobe · OAAdobe interview questions
  • Parcel Attribute Store with TTLAirbnb · OAAirbnb interview questions
  • Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
  • Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
  • Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions
  • Path Leaf Character IteratorGoogle · ONSITE INTERVIEWGoogle interview questions
  • Process On-Call Routing OperationsOpenAI · PHONE SCREENOpenAI interview questions

TikTok interview experiences

  • TikTok AI Infra Intern LLM Round 2: 1 Experience
  • TikTok AI Infra Intern DeepSpeed Round: 1 Experience
  • TikTok AI Infra Intern Kernel Round: 1 Experience
  • TikTok AI Infra Intern Interviews, 3 Rounds: 1 Experience
  • TikTok AI Infra Intern Interview Timeline: 1 Experience
  • TikTok AI Infra Intern Onsite Round 2: 1 Experience
All TikTok interview questionsSee TikTok hiring insights
Saved
class Solution {
    public int[] memoryAllocator(int[] memory, String[] operations) {
      // write your code here
    }

}
Checking account…