Skip to the workspace
Pebellslearn by building
Foundations: Complexity & Arrays  ›  Lesson

Arrays & the Playlist

Arrays 20 minContiguous slots: O(1) to read, O(n) to shift

dsa / Arrays & the Playlist

Explore each scene at your own pace.

01 · Drag track 1 to the top of the playlist

Drag track 1 to the top of the playlist

Arrays & the Playlist

Definition
An array stores items in one contiguous block of equal-sized slots, so the position of any index is found by arithmetic in constant time, while inserting or deleting in the middle forces later items to shift.
In plain words
Picture a shelf of numbered boxes, all the same size, side by side. You can walk straight to box 7 without checking the others, but to make room between box 2 and box 3 you must slide every box after it along.

In short: an array keeps its items in one contiguous block, same-size slots back to back, so the computer reaches slot i by arithmetic — address of slot 0 + i × slot size — never by searching. That's why reading or writing any index is O(1). The same contiguity means slots can't have gaps: inserting or deleting in the middle forces every item after it to shift, which is O(n). Python's list is this structure, made dynamic by tracking spare capacity so append is usually O(1) too.

A shelf of numbered slots holding track names; an arrow does one jump straight to slot 2, labelled 'address of slot 0 + 2 × slot size — O(1), no searching'

Drag track 1 to the top of the playlist

python

Reading is free arithmetic. Dragging a track is a different story — slots can't have gaps, so opening one at index 1 means shifting everything after it one slot right, back to front so nothing is overwritten before it's moved:

your code, drawn · array

Every item after the insert point moves once, back to front. Change i or new and re-run to count the moves yourself.

Deleting mirrors it — everything after the gap shifts left, front to back, so each slot is read before it's overwritten:

watch it move · array

Deleting slot 0 moves every later track one slot left, which is why pop(0) costs O(n) and pop() costs nothing.

del playlist[0] leaves a hole at the front, and slots can't have gaps. 4 cells. Error at index 0.

1 / 5

The mistake: treating insert/pop(0)/del playlist[0] as cheap because append/pop() are. Near the end that's a move or two; at the front it's all n. Shift in the wrong direction and one track overwrites the rest:

broken — fix it

This should open a gap at index 1 for "Echo". Instead "Tide" is copied into every later slot and "Ember" is lost.

❓ Cross-question — "A list can hold [1, "Glow", 4.5]. How can every slot be the same size?" Slots don't hold the objects — they hold references to objects living elsewhere, and every reference is the same size (an ArrayList<Object>, in Java terms).

Scene 1 of 8
Project context and further resources
You're building a piece ofTunebox — the library
This piece — move_track(): The ordered list of tracks — add, remove, reorder.
Scenario The playlist is Tunebox's ordered list of tracks — users add, remove and drag tracks around all day. Knowing which of those operations are O(1) and which shift every track is what keeps a 5,000-track playlist smooth.
move_track.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
move_track(playlist, i, j) → listA new playlist with the track at i moved to position j.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.