DSAArraysStrings
Arrays and Strings: The Foundation
Deep dive into array and string manipulation techniques every developer should know.
5 min read
Arrays and Strings: The Foundation
Arrays and strings are the most fundamental data structures. Mastering them is essential for technical interviews and everyday programming.
Array Fundamentals
Key Operations
- Access: O(1)
- Search: O(n)
- Insert at end: O(1) amortized
- Insert at index: O(n)
Common Patterns
Two Pointers
function isPalindrome(s) {
let left = 0, right = s.length - 1;
while (left < right) {
if (s[left] !== s[right]) return false;
left++;
right--;
}
return true;
}
Sliding Window
function maxSum(arr, k) {
let sum = 0;
for (let i = 0; i < k; i++) sum += arr[i];
let maxSum = sum;
for (let i = k; i < arr.length; i++) {
sum = sum - arr[i - k] + arr[i];
maxSum = Math.max(maxSum, sum);
}
return maxSum;
}
String Manipulation
Essential Methods
const s = "hello world";
s.split(' '); // ['hello', 'world']
s.substring(0, 5); // 'hello'
s.includes('world'); // true
s.indexOf('o'); // 4
Practice Problems
- Reverse a string in-place
- Find all anagrams in a string
- Longest substring without repeating characters
Master these patterns and you'll handle 80% of array/string problems.