DSA Tutorial
🔍

Array Practice Problems

Array Practice Problems

Master arrays through carefully selected problems that cover all essential patterns and techniques. Each problem is chosen to teach specific concepts and build your problem-solving skills.

Goal: Practice array manipulation, apply learned techniques, and build confidence for coding interviews.

Practice Strategy: Start with Easy problems to build fundamentals, move to Medium to learn patterns, tackle Hard to master advanced techniques. Aim for understanding, not just solving!

How to Use This Guide

Problem Format:

Each problem includes:

  • Difficulty Level: Easy, Medium, or Hard
  • Topics: Techniques required
  • Problem Statement: What you need to solve
  • Input/Output Examples: Test your understanding
  • Hints: Progressive clues if stuck
  • Expected Complexity: Time and space targets

Solving Approach:

  1. Read carefully - Understand inputs, outputs, constraints
  2. Think first - Plan approach before coding
  3. Start simple - Brute force, then optimize
  4. Test edge cases - Empty, single element, duplicates
  5. Analyze complexity - Meet the expected targets

Easy Problems (10)

Problem 1: Two Sum

Difficulty: Easy
Topics: Hash Map, Array Traversal
LeetCode: #1

Problem: Given an array of integers and a target, return indices of two numbers that add up to target.

Example:

Python
1# Input 2nums = [2, 7, 11, 15] 3target = 9 4 5# Output 6[0, 1] # nums[0] + nums[1] = 2 + 7 = 9

Constraints:

  • Each input has exactly one solution
  • Can't use same element twice

Hint 1: Brute force with nested loops is O(n²)
Hint 2: Use hash map to store seen numbers
Hint 3: For each number, check if (target - number) exists in map

Expected: Time O(n), Space O(n)

Problem 2: Best Time to Buy and Sell Stock

Difficulty: Easy
Topics: Kadane's Algorithm, Greedy
LeetCode: #121

Problem: Find maximum profit from buying and selling stock once. Buy before sell.

Example:

Python
1# Input 2prices = [7, 1, 5, 3, 6, 4] 3 4# Output 55 # Buy at 1, sell at 6 (profit = 5)

Hint 1: Track minimum price seen so far
Hint 2: At each day, calculate profit if selling today
Hint 3: Similar to maximum subarray problem

Expected: Time O(n), Space O(1)

Problem 3: Contains Duplicate

Difficulty: Easy
Topics: Hash Set, Sorting
LeetCode: #217

Problem: Check if array contains any duplicates.

Example:

Python
1# Input 2nums = [1, 2, 3, 1] 3 4# Output 5True # 1 appears twice

Hint 1: Use hash set to track seen numbers
Hint 2: If number already in set, found duplicate
Hint 3: Alternative: sort and check adjacent elements

Expected: Time O(n), Space O(n) with hash set

Problem 4: Maximum Subarray

Difficulty: Easy
Topics: Kadane's Algorithm, Dynamic Programming
LeetCode: #53

Problem: Find contiguous subarray with largest sum.

Example:

Python
1# Input 2nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4] 3 4# Output 56 # Subarray [4, -1, 2, 1]

Hint 1: Use Kadane's algorithm
Hint 2: At each position, decide: extend or start fresh?
Hint 3: current_sum = max(num, current_sum + num)

Expected: Time O(n), Space O(1)

Problem 5: Merge Sorted Array

Difficulty: Easy
Topics: Two Pointers, In-place Modification
LeetCode: #88

Problem: Merge two sorted arrays into first array (has enough space).

Example:

Python
1# Input 2nums1 = [1, 2, 3, 0, 0, 0] # m = 3 3nums2 = [2, 5, 6] # n = 3 4 5# Output (modify nums1 in-place) 6nums1 = [1, 2, 2, 3, 5, 6]

Hint 1: Fill from the end to avoid overwriting
Hint 2: Use three pointers: end of nums1, end of nums2, end of result
Hint 3: Compare and place larger element at the back

Expected: Time O(m + n), Space O(1)

Problem 6: Plus One

Difficulty: Easy
Topics: Array Manipulation, Math
LeetCode: #66

Problem: Increment large integer represented as array of digits.

Example:

Python
1# Input 2digits = [1, 2, 3] 3 4# Output 5[1, 2, 4] # 123 + 1 = 124

Hint 1: Start from the last digit
Hint 2: Handle carry propagation (9 → 0, carry 1)
Hint 3: Edge case: [9, 9, 9] → [1, 0, 0, 0]

Expected: Time O(n), Space O(1) or O(n) if new array

Problem 7: Move Zeroes

Difficulty: Easy
Topics: Two Pointers, In-place
LeetCode: #283

Problem: Move all zeros to end while maintaining relative order.

Example:

Python
1# Input 2nums = [0, 1, 0, 3, 12] 3 4# Output (in-place) 5nums = [1, 3, 12, 0, 0]

Hint 1: Use two pointers: one for reading, one for writing
Hint 2: Write non-zero elements first
Hint 3: Fill remaining with zeros

Expected: Time O(n), Space O(1)

Problem 8: Remove Duplicates from Sorted Array

Difficulty: Easy
Topics: Two Pointers, In-place
LeetCode: #26

Problem: Remove duplicates from sorted array in-place. Return new length.

Example:

Python
1# Input 2nums = [1, 1, 2] 3 4# Output 52 # nums becomes [1, 2, _]

Hint 1: Sorted array = duplicates are adjacent
Hint 2: Use two pointers: slow for unique position, fast for scanning
Hint 3: When different, copy and advance slow

Expected: Time O(n), Space O(1)

Problem 9: Single Number

Difficulty: Easy
Topics: Bit Manipulation, XOR
LeetCode: #136

Problem: Find the number that appears once (all others appear twice).

Example:

Python
1# Input 2nums = [2, 2, 1] 3 4# Output 51 # Only 1 appears once

Hint 1: XOR has useful properties: a ⊕ a = 0, a ⊕ 0 = a
Hint 2: XOR all numbers: duplicates cancel out
Hint 3: Result is the single number

Expected: Time O(n), Space O(1)

Problem 10: Intersection of Two Arrays II

Difficulty: Easy
Topics: Hash Map, Two Pointers
LeetCode: #350

Problem: Find intersection of two arrays (with duplicates).

Example:

Python
1# Input 2nums1 = [1, 2, 2, 1] 3nums2 = [2, 2] 4 5# Output 6[2, 2]

Hint 1: Use hash map to count occurrences
Hint 2: For each element in nums2, check if in map and count > 0
Hint 3: Alternative: sort both, use two pointers

Expected: Time O(n + m), Space O(min(n, m))

Medium Problems (10)

Problem 11: Product of Array Except Self

Difficulty: Medium
Topics: Prefix/Suffix Product, Array
LeetCode: #238

Problem: Return array where result[i] = product of all except nums[i]. No division allowed.

Example:

Python
1# Input 2nums = [1, 2, 3, 4] 3 4# Output 5[24, 12, 8, 6] # [2*3*4, 1*3*4, 1*2*4, 1*2*3]

Hint 1: result[i] = (product of left side) × (product of right side)
Hint 2: Build prefix product array, then suffix product array
Hint 3: Can optimize to O(1) space by building result in-place

Expected: Time O(n), Space O(1) excluding output

Problem 12: Container With Most Water

Difficulty: Medium
Topics: Two Pointers, Greedy
LeetCode: #11

Problem: Find two lines that form container with most water.

Example:

Python
1# Input 2height = [1, 8, 6, 2, 5, 4, 8, 3, 7] 3 4# Output 549 # Lines at index 1 and 8 (height 8 and 7, width 7)

Hint 1: Water amount = min(height[i], height[j]) × (j - i)
Hint 2: Start with widest container (leftmost and rightmost)
Hint 3: Move pointer at shorter line inward

Expected: Time O(n), Space O(1)

Problem 13: 3Sum

Difficulty: Medium
Topics: Two Pointers, Sorting
LeetCode: #15

Problem: Find all unique triplets that sum to zero.

Example:

Python
1# Input 2nums = [-1, 0, 1, 2, -1, -4] 3 4# Output 5[[-1, -1, 2], [-1, 0, 1]]

Hint 1: Sort array first
Hint 2: Fix one number, use two pointers for remaining two
Hint 3: Skip duplicates to avoid duplicate triplets

Expected: Time O(n²), Space O(1) excluding output

Problem 14: Next Permutation

Difficulty: Medium
Topics: Array Manipulation, Two Pointers
LeetCode: #31

Problem: Find next lexicographically greater permutation.

Example:

Python
1# Input 2nums = [1, 2, 3] 3 4# Output (in-place) 5nums = [1, 3, 2]

Hint 1: Find first decreasing element from right
Hint 2: Swap with next larger element
Hint 3: Reverse suffix to get smallest arrangement

Expected: Time O(n), Space O(1)

Problem 15: Rotate Array

Difficulty: Medium
Topics: Array Reversal, Cyclic Replacement
LeetCode: #189

Problem: Rotate array to the right by k steps.

Example:

Python
1# Input 2nums = [1, 2, 3, 4, 5, 6, 7] 3k = 3 4 5# Output (in-place) 6nums = [5, 6, 7, 1, 2, 3, 4]

Hint 1: Reverse entire array
Hint 2: Reverse first k elements
Hint 3: Reverse remaining elements

Expected: Time O(n), Space O(1)

Problem 16: Find All Duplicates

Difficulty: Medium
Topics: Array Marking, Index as Hash
LeetCode: #442

Problem: Find all duplicates in array where 1 ≤ a[i] ≤ n.

Example:

Python
1# Input 2nums = [4, 3, 2, 7, 8, 2, 3, 1] 3 4# Output 5[2, 3]

Hint 1: Use indices as markers (numbers ≤ n)
Hint 2: Mark visited by negating value at index
Hint 3: If already negative, found duplicate

Expected: Time O(n), Space O(1)

Problem 17: Subarray Sum Equals K

Difficulty: Medium
Topics: Prefix Sum, Hash Map
LeetCode: #560

Problem: Count number of continuous subarrays that sum to k.

Example:

Python
1# Input 2nums = [1, 1, 1] 3k = 2 4 5# Output 62 # [1, 1] and [1, 1]

Hint 1: Use prefix sum with hash map
Hint 2: If prefix_sum - k exists in map, found subarray
Hint 3: Store count of each prefix sum

Expected: Time O(n), Space O(n)

Problem 18: Longest Consecutive Sequence

Difficulty: Medium
Topics: Hash Set, Union Find
LeetCode: #128

Problem: Find length of longest consecutive sequence (unsorted array).

Example:

Python
1# Input 2nums = [100, 4, 200, 1, 3, 2] 3 4# Output 54 # Sequence [1, 2, 3, 4]

Hint 1: Put all numbers in hash set
Hint 2: For each number that's a sequence start (n-1 not in set)
Hint 3: Count consecutive numbers

Expected: Time O(n), Space O(n)

Problem 19: Spiral Matrix

Difficulty: Medium
Topics: Matrix Traversal, Simulation
LeetCode: #54

Problem: Return elements of matrix in spiral order.

Example:

Python
1# Input 2matrix = [ 3 [1, 2, 3], 4 [4, 5, 6], 5 [7, 8, 9] 6] 7 8# Output 9[1, 2, 3, 6, 9, 8, 7, 4, 5]

Hint 1: Use four boundaries: top, bottom, left, right
Hint 2: Traverse right, down, left, up, then shrink boundaries
Hint 3: Continue until boundaries cross

Expected: Time O(m × n), Space O(1)

Problem 20: Jump Game

Difficulty: Medium
Topics: Greedy, Dynamic Programming
LeetCode: #55

Problem: Determine if you can reach the last index.

Example:

Python
1# Input 2nums = [2, 3, 1, 1, 4] 3 4# Output 5True # Jump 1 step to index 1, then 3 steps to last

Hint 1: Track maximum reachable index
Hint 2: At each position, update max reachable
Hint 3: If current index > max reachable, can't proceed

Expected: Time O(n), Space O(1)

Hard Problems (5)

Problem 21: Trapping Rain Water

Difficulty: Hard
Topics: Two Pointers, Dynamic Programming
LeetCode: #42

Problem: Calculate how much rainwater can be trapped.

Example:

Python
1# Input 2height = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1] 3 4# Output 56 # Total water trapped

Hint 1: Water at position i = min(max_left, max_right) - height[i]
Hint 2: Use two pointers from both ends
Hint 3: Move pointer at shorter side

Expected: Time O(n), Space O(1)

Problem 22: First Missing Positive

Difficulty: Hard
Topics: Array Marking, Index as Hash
LeetCode: #41

Problem: Find smallest missing positive integer.

Example:

Python
1# Input 2nums = [3, 4, -1, 1] 3 4# Output 52 # Smallest missing positive

Hint 1: Answer must be in range [1, n+1]
Hint 2: Place each number at its corresponding index
Hint 3: First index without correct value is answer

Expected: Time O(n), Space O(1)

Problem 23: Median of Two Sorted Arrays

Difficulty: Hard
Topics: Binary Search, Merge Sort
LeetCode: #4

Problem: Find median of two sorted arrays in O(log(m+n)).

Example:

Python
1# Input 2nums1 = [1, 3] 3nums2 = [2] 4 5# Output 62.0 # Merged: [1, 2, 3], median = 2

Hint 1: Binary search on smaller array
Hint 2: Partition both arrays so left half ≤ right half
Hint 3: Median is from boundary elements

Expected: Time O(log(min(m, n))), Space O(1)

Problem 24: Sliding Window Maximum

Difficulty: Hard
Topics: Sliding Window, Deque
LeetCode: #239

Problem: Find maximum in each sliding window of size k.

Example:

Python
1# Input 2nums = [1, 3, -1, -3, 5, 3, 6, 7] 3k = 3 4 5# Output 6[3, 3, 5, 5, 6, 7]

Hint 1: Use deque to store indices
Hint 2: Keep deque in decreasing order
Hint 3: Remove indices outside window

Expected: Time O(n), Space O(k)

Problem 25: Longest Substring with At Most K Distinct

Difficulty: Hard
Topics: Sliding Window, Hash Map
LeetCode: #340 (Premium)

Problem: Find longest substring with at most K distinct characters.

Example:

Python
1# Input 2s = "eceba" 3k = 2 4 5# Output 63 # "ece" has 2 distinct chars

Hint 1: Use sliding window with hash map
Hint 2: Expand window while distinct ≤ k
Hint 3: Shrink from left when distinct > k

Expected: Time O(n), Space O(k)

Study Plan

Week 1: Easy Problems (Build Foundation)

  • Days 1-2: Problems 1-3 (Hash Map basics)
  • Days 3-4: Problems 4-6 (Array manipulation)
  • Days 5-7: Problems 7-10 (Two pointers)

Week 2: Medium Problems (Learn Patterns)

  • Days 1-2: Problems 11-12 (Prefix/Two pointers)
  • Days 3-4: Problems 13-15 (Sorting techniques)
  • Days 5-7: Problems 16-20 (Advanced patterns)

Week 3: Hard Problems (Master Skills)

  • Days 1-2: Problem 21 (Trapping water)
  • Days 3-4: Problems 22-23 (Advanced techniques)
  • Days 5-7: Problems 24-25 + Review

Key Takeaways

Essential Array Patterns:

Hash Map Pattern: Two Sum, Contains Duplicate, Intersection

  • Use for O(1) lookup
  • Trade space for time

Two Pointers Pattern: Move Zeroes, 3Sum, Container

  • Start from both ends or same side
  • Move based on conditions

Sliding Window Pattern: Subarray Sum, Longest Substring

  • Expand window to right
  • Shrink from left when needed

Kadane's Algorithm: Maximum Subarray

  • Key decision: extend or start fresh
  • O(n) for maximum subarray sum

Prefix Sum Pattern: Product Except Self, Subarray Sum K

  • Precompute cumulative values
  • Answer queries in O(1)

Array Marking Pattern: Find Duplicates, First Missing Positive

  • Use indices as hash when 1 ≤ nums[i] ≤ n
  • Mark by negating or swapping

Practice Tips:

  1. Start with brute force
  2. Optimize with patterns
  3. Test edge cases thoroughly
  4. Analyze time/space complexity
  5. Code multiple solutions

Remember: Patterns repeat across problems - master the pattern, solve many problems!

Additional Resources

Online Judges:

  • LeetCode: Best for interviews
  • HackerRank: Good for beginners
  • Codeforces: Competitive programming
  • GeeksforGeeks: Detailed explanations

Topics to Master:

  1. Two Pointers
  2. Sliding Window
  3. Prefix Sum
  4. Hash Map/Set
  5. Array Manipulation
  6. Sorting Techniques
  7. Binary Search on Arrays
  8. Dynamic Programming with Arrays

Start practicing and build your problem-solving skills! 🚀

Array Practice Problems