skip to content

questions

4

In an array-backed alert feed, why is inserting at index 0 O(n) but appending O(1)?

level: middleimportance: must knowfreq 76%

basics

~20 s

An array's slots are fixed positions, so making room at index 0 means moving every existing element one slot right — n moves. Appending writes into the first free slot and moves nothing, so it costs one write.

open as a page

Removing one element from an array: when may you swap with the last instead of shifting?

level: middleimportance: should knowfreq 50%

basics

~20 s

Only when the array's order carries no meaning. Swapping the last element into the vacated slot is constant time but permutes the array; shifting the tail left costs Θ(n − i) and is the only order-preserving option.

open as a page

A diff inserts each match result into a sorted score array per event — what do you flag in review?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Each insert opens a hole mid-array, so it costs linear time in the scores already stored. Across m events that compounds to quadratic total work — invisible at fifty players, a stall at fifty thousand.

open as a page