FastPrepDefragment a Buffer of Objects
Problems/Defragment a Buffer of Objects

Defragment a Buffer of Objects

Hudson River Trading logoHudson River Trading● EasyNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report comes from a Hudson River Trading new grad phone round for a C++ developer role. Its third question was a template function. You get the start of a buffer of same-type objects and a list of pointers to all of them. You move the objects into one solid block at the front, fix the pointers, and return the size of that block. The report says the buffer has room for all the objects and that every object starts at or after the buffer start.

The task text does not say which object should go first. The author's own answer moved object i into slot i, in list order. But doing that inside the buffer can overwrite an object that has not moved yet. This version differs on purpose: the objects keep their memory order. The buffer is a list of integer cells, each object takes objectSize cells, and you return the size, the new pointers and the packed cells instead of changing the buffer in place.

Our version is about 75% the same as the reported question.

A buffer of memory is modeled as an integer array memory. Each element is one cell, and cell 0 is the start of the buffer.

The buffer holds several objects of the same type. Each object takes exactly objectSize cells in a row: an object that starts at cell p uses cells p to p + objectSize - 1. No two objects share a cell. Cells that belong to no object are free, and their values do not matter.

You are also given an array pointers with one entry per object: pointers[i] is the cell where object i starts. Every object in the buffer is listed exactly once.

Defragment the buffer. Move the objects so they sit side by side at the start of the buffer, with no free cells between them, and update the pointers:

  • Keep the objects in memory order. The object that starts closest to cell 0 moves to cell 0, the object with the next smallest start moves to cell objectSize, the one after that to cell 2 * objectSize, and so on.
  • A move copies all of an object's cells, unchanged and in the same order.
  • After the move, pointers[i] is the new start cell of object i. The entries of pointers stay in the same order.

Return an array of three rows:

  1. [size], where size = pointers.length * objectSize is the number of cells in the packed block.
  2. The updated pointers.
  3. The packed block: cells 0 to size - 1 of the buffer after the move.

Cells after the packed block are not part of the answer. You may build the answer in a new array or move the cells inside memory itself.

Function

defragment(pointers: int[], memory: int[], objectSize: int) → int[][]

Examples

Example 1

pointers = [5,1]memory = [0,10,11,0,0,20,21,0]objectSize = 2return = [[4],[2,0],[10,11,20,21]]

Each object takes 2 cells. Object 0 starts at cell 5 and holds 20, 21. Object 1 starts at cell 1 and holds 10, 11.

  • In memory order, object 1 comes first, so it moves to cells 0 and 1.
  • Object 0 comes next, so it moves to cells 2 and 3.

The pointers become [2, 0]. The packed block has 2 * 2 = 4 cells: [10, 11, 20, 21]. Object 0 is first in pointers but second in the block, because the block follows memory order.

Example 2

pointers = [6,2]memory = [0,0,1,2,3,0,4,5,6]objectSize = 3return = [[6],[3,0],[1,2,3,4,5,6]]

Each object takes 3 cells. Object 1 starts at cell 2 and holds 1, 2, 3. Object 0 starts at cell 6 and holds 4, 5, 6.

  • Object 1 comes first in memory, so it moves to cells 0 to 2. Its new place shares cell 2 with its old place.
  • Object 0 moves to cells 3 to 5.

The pointers become [3, 0], and the packed block is [1, 2, 3, 4, 5, 6] with size 6.

Example 3

pointers = [0,7,3]memory = [5,6,0,1,2,0,0,3,4,0]objectSize = 2return = [[6],[0,4,2],[5,6,1,2,3,4]]

Each object takes 2 cells. In memory order the objects are object 0 (cell 0), object 2 (cell 3) and object 1 (cell 7).

  • Object 0 is already at cell 0, so it stays.
  • Object 2 moves from cell 3 to cell 2.
  • Object 1 moves from cell 7 to cell 4.

The pointers become [0, 4, 2], and the packed block is [5, 6, 1, 2, 3, 4].

Constraints

  • 1 <= memory.length <= 2000
  • -10^9 <= memory[i] <= 10^9
  • 1 <= objectSize <= memory.length
  • 1 <= pointers.length <= memory.length
  • 0 <= pointers[i] <= memory.length - objectSize
  • No two objects share a cell: for i != j, |pointers[i] - pointers[j]| >= objectSize.

More Hudson River Trading Phone Screen questions

  • Format a Signed Integer with CommasPHONE SCREEN · Seen Nov 2026
  • Order and Batch Open-Close LifecyclePHONE SCREEN · Seen Nov 2026
  • Tilting Maze BallPHONE SCREEN · Seen Oct 2026
  • Tiered Expiring Item StorePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Market Data Signal WatcherPHONE SCREEN · Seen Jul 2026
  • Integer to String Without Built-insPHONE SCREEN · Seen May 2026
  • Contact Tracing Through Timed MeetingsPHONE SCREEN · Seen Nov 2024
  • Polydivisible Pandigital NumbersPHONE SCREEN · Seen Feb 2024
  • Read N Bytes with read4096 Across CallsPHONE SCREEN · Seen Oct 2023
  • Call Auction Clearing Price and Order FillsPHONE SCREEN · Seen Nov 2022

Similar questions at other companies

  • Majority ElementByteDance · PHONE SCREENByteDance interview questions
  • Recommend Films from Viewers Who Watched Everything You DidBloomberg LP · PHONE SCREENBloomberg LP interview questions
  • Total Years of Work ExperienceLinkedIn · PHONE SCREENLinkedIn interview questions
  • Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
  • Employee Count Median, Mean and ModeClay · PHONE SCREEN
  • Squares of a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
  • Assign CookiesAmazon · PHONE SCREEN
  • Sort by Checking PermutationsMercor · PHONE SCREENMercor interview questions
  • Meeting RoomsGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
  • Parse and Sort Integer DataLegora · PHONE SCREEN

Hudson River Trading interview experiences

  • Hudson River Trading Quant Video Interview: 1 Experience
  • HRT SWE Intern and Onsites: 34 Experiences (Part 2 of 2)
  • HRT Python Track Interviews: 64 Experiences (Part 1 of 2)
  • HRT New Grad Python Track Interviews: 9 Experiences
All Hudson River Trading interview questionsSee Hudson River Trading hiring insights
Saved
class Solution {
    public int[][] defragment(int[] pointers, int[] memory, int objectSize) {
        // Write your code here.
    }
}
Checking account…