Prepare for coding interviews with 40+ questions on arrays, strings, and essential manipulation techniques.
Methods: (1) HashSet - O(n) time, O(n) space, add elements and check existence, (2) Sort first - O(n log n) time, O(1) space, compare adjacent, (3) Floyd's cycle detection for specific cases, (4) XOR for finding single duplicate in 1-n range. Choose based on constraints: space-limited use sorting, time-critical use HashSet.
Two pointers traverse array from different positions. Patterns: (1) Opposite ends - start/end moving inward (sorted array problems, palindrome), (2) Same direction - slow/fast pointers (remove duplicates, linked list cycle), (3) Two arrays - pointer per array (merge sorted). Key insight: reduces O(n²) brute force to O(n). Works best on sorted arrays.
Sliding window maintains a subset of elements as window slides across array. Types: (1) Fixed size - sum of k elements, (2) Variable size - expand/contract based on condition. Pattern: expand right pointer, contract left when condition violated, track result. Use for: subarray sums, longest substring without repeating chars. Converts O(n²) to O(n).
Two-pointer approach: left at start, right at end, swap and move inward until they meet. O(n) time, O(1) space. For char array: swap arr[left] with arr[right]. In languages with immutable strings (Java, Python): convert to array, reverse, convert back. Consider: Unicode handling, surrogate pairs in UTF-16.
Kadane's finds maximum sum contiguous subarray in O(n). Logic: at each position, either extend current subarray or start new one. Code: maxEndingHere = max(arr[i], maxEndingHere + arr[i]), maxSoFar = max(maxSoFar, maxEndingHere). Handles negatives. Variants: track indices, circular array (compute min subarray), 2D extension.
Practice with interactive quizzes and get instant feedback.
Start Free Practice