ArraysCore idea
An array is one continuous block of memory holding elements of the same type. Because every slot is the same size, the computer can jump straight to any index: that is why access is O(1). Inserting or deleting in the middle is O(n), because every element after it has to shift.
Time complexity
| Operation | Time | Why |
|---|---|---|
| Access by index | O(1) | Jump straight to the slot |
| Search (unsorted) | O(n) | Look at every element |
| Search (sorted) | O(log n) | Binary search halves the range |
| Insert at end | O(1) amortized | Occasional resize, usually free |
| Insert at beginning | O(n) | Everything shifts right |
| Delete at index | O(n) | Everything after it shifts left |
A common pattern: Two SumPattern
The classic array question. Instead of checking every pair (O(n²)), remember each number you have seen in a hash map, and for every new number ask: have I already seen the value that completes the pair?
function twoSum(nums: number[], target: number): number[] {
const map = new Map<number, number>();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (map.has(complement)) return [map.get(complement)!, i];
map.set(nums[i], i);
}
return [];
}StringsCore idea
A string is an array of characters. In most languages strings are immutable: you cannot change one in place, so every modification creates a new string and costs O(n). Python strings are immutable; in Java, use StringBuilder to build a string efficiently.
String techniques
Character frequency
Count letters with an array of size 26 (lowercase only) or a hash map.
Reversal
Two pointers from both ends, swapping until they meet. O(n) time, O(1) space.
Anagram check
Sort both strings and compare, or compare their character counts.
Palindrome check
Two pointers from both ends, comparing characters as they move inward.
function isPalindrome(s: string): boolean {
let left = 0, right = s.length - 1;
while (left < right) {
if (s[left] !== s[right]) return false;
left++;
right--;
}
return true;
}In-place array operationsPattern
Interviewers often ask for O(1) extra space. The trick is to use the array itself as your workspace: one pointer reads, another writes over values you no longer need.
function removeDuplicates(nums: number[]): number {
let write = 1;
for (let read = 1; read < nums.length; read++) {
if (nums[read] !== nums[read - 1]) {
nums[write] = nums[read];
write++;
}
}
return write;
}Common mistakesPitfalls
In the interviewIn the interview
Before writing any array code, ask the questions that decide the approach:
Each answer points to a different pattern: sorted suggests two pointers or binary search, unsorted with lookups suggests a hash map, and “in place” suggests read/write pointers.
Frequently asked questionsFAQ
Are strings and arrays the same in coding interviews?
Strings are arrays of characters, but in most languages they are immutable, so concatenation costs O(n) and needs a builder like StringBuilder. The key interview distinction is mutation: arrays support in-place edits while strings require rebuilding, which changes the optimal approach to the problem.
What is the time complexity of array insertion and deletion?
Inserting or deleting at the end of an array is O(1) amortized, while the same at the beginning or middle is O(n) because elements must shift. Random access by index is O(1), searching an unsorted array is O(n), and a sorted array supports O(log n) binary search.
How do you check whether two strings are anagrams?
Use a character frequency count: increment counts for one string and decrement for the other, then confirm every count is zero. That is O(n) time and O(1) space for lowercase letters using a 26-size array, versus O(n log n) for sorting both strings and comparing.

Now explain arrays and strings to me out loud
I'll give you interview problems and ask follow-ups, like a real interviewer.

