Note top is always available. In top: press M to sort by memory, P by CPU, k to kill a process, q to quit. htop is a friendlier alternative with mouse support, color, and tree view but may need installing. -u filters by user.
total used free shared buff/cache available
Mem: 16Gi 5.2Gi 2.1Gi 312Mi 8.7Gi 10Gi
Swap: 4.0Gi 128Mi 3.9Gi
Note -h for human-readable. The 'available' column is the best indicator of how much memory is actually free for applications (it includes reclaimable buffer/cache). Linux aggressively caches, so low 'free' is normal and does not mean you are out of memory. Not available on macOS; use vm_stat instead.
Measure extra memory used beyond the input.
- Variables / pointers: O(1)
- New array of size n: O(n)
- Recursive call stack of depth n: O(n)
- 2D matrix n×m: O(n*m)
example
// JavaScript// O(1) space — in-place swapfunction reverseInPlace(arr) {
let l = 0, r = arr.length - 1;
while (l < r) {
[arr[l], arr[r]] = [arr[r], arr[l]];
l++; r--;
}
}
// O(n) space — new arrayfunction reversed(arr) {
return arr.slice().reverse();
}
# Pythondef reverse_in_place(arr):
l, r = 0, len(arr) - 1while l < r:
arr[l], arr[r] = arr[r], arr[l]
l += 1; r -= 1def reversed_copy(arr):
return arr[::-1] # O(n) space
output
In-place: O(1) space | Copy: O(n) space
Note Interviewers often ask 'Can you do it in O(1) space?' — this means modify the input in place. Recursive solutions use O(depth) stack space even if no extra data structures are created. Always mention auxiliary space vs total space.