Top Array and String Problems
In this page:
Two Sum
Two Sum asks you to find two numbers in an array that add up to a given target — the naive approach checks every pair in O(n²), but storing seen values in a hash map as you scan brings it down to a single O(n) pass.
Example: Two Sum
#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
int arr[] = {2,7,11,15}, target = 9;
unordered_map<int,int> seen;
for (int i = 0; i < 4; i++) {
if (seen.count(target - arr[i])) { cout << "Indices: " << seen[target-arr[i]] << "," << i; break; }
seen[arr[i]] = i;
}
return 0;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
int[] arr = {2,7,11,15};
int target = 9;
Map<Integer,Integer> seen = new HashMap<>();
for (int i = 0; i < 4; i++) {
if (seen.containsKey(target - arr[i])) { System.out.println("Indices: " + seen.get(target-arr[i]) + "," + i); break; }
seen.put(arr[i], i);
}
}
}
arr = [2,7,11,15]
target = 9
seen = {}
for i, x in enumerate(arr):
if target - x in seen:
print(f"Indices: {seen[target-x]},{i}")
break
seen[x] = i
#include <stdio.h>
int main() {
int arr[] = {2,7,11,15}, target = 9;
for (int i = 0; i < 4; i++)
for (int j = i+1; j < 4; j++)
if (arr[i]+arr[j] == target) { printf("Indices: %d,%d", i, j); return 0; }
return 0;
}
Login to try C/C++/Java code in the editor
Maximum and Minimum
Finding the maximum or minimum value in an array only requires one linear scan, tracking the best value seen so far and updating it whenever a more extreme value appears — no sorting or extra data structure needed.
Example: Maximum and Minimum
#include <iostream>
using namespace std;
int main() {
int arr[] = {3,7,1,9,4};
int mx = arr[0], mn = arr[0];
for (int i = 1; i < 5; i++) { if (arr[i] > mx) mx = arr[i]; if (arr[i] < mn) mn = arr[i]; }
cout << "Max: " << mx << " Min: " << mn;
return 0;
}
public class Main {
public static void main(String[] args) {
int[] arr = {3,7,1,9,4};
int mx = arr[0], mn = arr[0];
for (int i = 1; i < 5; i++) { if (arr[i] > mx) mx = arr[i]; if (arr[i] < mn) mn = arr[i]; }
System.out.println("Max: " + mx + " Min: " + mn);
}
}
arr = [3,7,1,9,4]
mx = mn = arr[0]
for x in arr[1:]:
if x > mx: mx = x
if x < mn: mn = x
print("Max:", mx, "Min:", mn)
#include <stdio.h>
int main() {
int arr[] = {3,7,1,9,4};
int mx = arr[0], mn = arr[0];
for (int i = 1; i < 5; i++) { if (arr[i] > mx) mx = arr[i]; if (arr[i] < mn) mn = arr[i]; }
printf("Max: %d Min: %d", mx, mn);
return 0;
}
Login to try C/C++/Java code in the editor
Array Search
Linear search checks every element in order until it finds a match or reaches the end, which is simple and always correct but takes O(n) time — a strong contrast to binary search's O(log n), which requires the data to be sorted first.
Example: Array Search
#include <iostream>
using namespace std;
int main() {
int arr[] = {5,3,8,1,9}, target = 8;
for (int i = 0; i < 5; i++) if (arr[i] == target) { cout << "Found at index " << i << " in O(n)"; break; }
return 0;
}
public class Main {
public static void main(String[] args) {
int[] arr = {5,3,8,1,9};
int target = 8;
for (int i = 0; i < 5; i++) if (arr[i] == target) { System.out.println("Found at index " + i + " in O(n)"); break; }
}
}
arr = [5,3,8,1,9]
target = 8
for i, x in enumerate(arr):
if x == target:
print(f"Found at index {i} in O(n)")
break
#include <stdio.h>
int main() {
int arr[] = {5,3,8,1,9}, target = 8;
for (int i = 0; i < 5; i++) if (arr[i] == target) { printf("Found at index %d in O(n)", i); break; }
return 0;
}
Login to try C/C++/Java code in the editor
String Thinking
Many string problems reduce to counting how often each character appears (using a fixed-size array or hash map), then scanning or comparing those counts — this single technique underlies anagram checks, character-frequency puzzles, and more.
Example: String Thinking
#include <iostream>
using namespace std;
int main() {
string s1 = "listen", s2 = "silent";
int freq[26] = {0};
for (char c : s1) freq[c-'a']++;
for (char c : s2) freq[c-'a']--;
bool isAnagram = true;
for (int i = 0; i < 26; i++) if (freq[i] != 0) isAnagram = false;
cout << "'" << s1 << "' and '" << s2 << "' are anagrams: " << isAnagram;
return 0;
}
public class Main {
public static void main(String[] args) {
String s1 = "listen", s2 = "silent";
int[] freq = new int[26];
for (char c : s1.toCharArray()) freq[c-'a']++;
for (char c : s2.toCharArray()) freq[c-'a']--;
boolean isAnagram = true;
for (int f : freq) if (f != 0) isAnagram = false;
System.out.println("'" + s1 + "' and '" + s2 + "' are anagrams: " + isAnagram);
}
}
from collections import Counter
s1, s2 = "listen", "silent"
print(f"'{s1}' and '{s2}' are anagrams: {Counter(s1) == Counter(s2)}")
#include <stdio.h>
int main() {
char s1[] = "listen", s2[] = "silent";
int freq[26] = {0};
for (int i = 0; s1[i]; i++) freq[s1[i]-'a']++;
for (int i = 0; s2[i]; i++) freq[s2[i]-'a']--;
int isAnagram = 1;
for (int i = 0; i < 26; i++) if (freq[i] != 0) isAnagram = 0;
printf("'%s' and '%s' are anagrams: %s", s1, s2, isAnagram ? "true" : "false");
return 0;
}
Login to try C/C++/Java code in the editor
Interview Practice
A common interview strategy is to get a brute-force solution working first so you have something correct to test against, then look for repeated work or unnecessary comparisons that a better data structure or single pass could eliminate.
Example: Interview Practice
#include <iostream>
using namespace std;
int main() {
cout << "Get brute force working first, then look for repeated work a better approach can eliminate";
return 0;
}
public class Main {
public static void main(String[] args) {
System.out.println("Get brute force working first, then look for repeated work a better approach can eliminate");
}
}
print("Get brute force working first, then look for repeated work a better approach can eliminate")
#include <stdio.h>
int main() {
printf("Get brute force working first, then look for repeated work a better approach can eliminate");
return 0;
}
Login to try C/C++/Java code in the editor
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: