Chapter 63: DSA Exercises
Here is a detailed, practical and structured set of DSA Exercises — exactly the kind of list and explanations a good teacher would give you when you ask:
“Give me good exercises to really learn DSA.”
I divided them by topic, from beginner-friendly to medium-hard, with realistic difficulty order, what concept they teach, and why they are worth solving.
Each problem has:
- Name / LeetCode number (if available)
- Core concept it teaches
- Difficulty level (according to most students’ experience)
- Recommended order
Beginner Level Exercises (Must do first – build intuition)
- Find the maximum element in an array → Teaches: basic loop, comparison Difficulty: ★☆☆
- Find second largest element in array (without sorting) → Teaches: single pass, tracking two variables Difficulty: ★★☆
- Reverse an array in-place (without extra space) → Teaches: two pointers, swapping Difficulty: ★★☆
- Check if array is palindrome → Teaches: two pointers from both ends Difficulty: ★★☆
- Move all zeros to the end (maintain relative order of non-zeros) → Teaches: two pointers / one pass Difficulty: ★★☆ (LeetCode #283 – Move Zeroes)
- Remove duplicates from sorted array (in-place) → Teaches: two pointers, overwriting Difficulty: ★★☆ (LeetCode #26)
- Rotate array to the right by k steps (in-place) → Teaches: multiple approaches (reverse trick is best) Difficulty: ★★★ (LeetCode #189)
- Majority Element (element appears > n/2 times) → Teaches: Boyer-Moore Voting Algorithm (O(n) time, O(1) space) Difficulty: ★★★ (LeetCode #169)
- Best Time to Buy and Sell Stock (one transaction) → Teaches: single pass, keeping track of minimum so far Difficulty: ★★☆ (LeetCode #121)
- Kadane’s Algorithm – Maximum Subarray Sum → Teaches: dynamic programming one-liner intuition Difficulty: ★★★ (LeetCode #53)
Intermediate Level Exercises (very important – do 70–80% of these)
Two Pointers / Sliding Window
- 3Sum (find all triplets sum to zero) → Teaches: sorting + two pointers pattern Difficulty: ★★★★ (LeetCode #15)
- Container With Most Water → Teaches: two pointers moving from ends Difficulty: ★★★ (LeetCode #11)
- Longest Substring Without Repeating Characters → Teaches: sliding window + hash set/map Difficulty: ★★★★ (LeetCode #3)
- Minimum Window Substring → Teaches: sliding window with two pointers + counter Difficulty: ★★★★★ (LeetCode #76)
- Longest Repeating Character Replacement → Teaches: sliding window with character frequency Difficulty: ★★★★
Binary Search & Binary Search on Answer
- Search in Rotated Sorted Array → Teaches: modified binary search Difficulty: ★★★★ (LeetCode #33)
- Find Minimum in Rotated Sorted Array → Teaches: binary search on unsorted rotated array Difficulty: ★★★★ (LeetCode #153)
- Koko Eating Bananas → Teaches: binary search on answer pattern Difficulty: ★★★★ (LeetCode #875)
- Capacity To Ship Packages Within D Days → Teaches: binary search on answer (very common pattern) Difficulty: ★★★★ (LeetCode #1011)
- Aggresive Cows / Book Allocation / Painter’s Partition → Teaches: classic binary search on answer (very frequent in India placements)
Linked List
- Reverse a Linked List (iterative + recursive) → Teaches: pointer manipulation Difficulty: ★★☆
- Merge Two Sorted Lists → Teaches: dummy node technique Difficulty: ★★☆ (LeetCode #21)
- Detect Cycle in Linked List (Floyd’s cycle detection) → Teaches: fast & slow pointer Difficulty: ★★★ (LeetCode #141)
- Remove Nth Node From End of List → Teaches: two pointers with gap Difficulty: ★★★ (LeetCode #19)
- LRU Cache (design question) → Teaches: hash map + doubly linked list Difficulty: ★★★★★ (LeetCode #146)
Intermediate-Advanced Exercises (very high value)
- Merge k Sorted Lists → Teaches: priority queue / min-heap Difficulty: ★★★★ (LeetCode #23)
- Kth Largest Element in an Array → Teaches: quickselect / min-heap Difficulty: ★★★★ (LeetCode #215)
- Number of Islands (DFS/BFS on grid) → Teaches: graph traversal on matrix Difficulty: ★★★★ (LeetCode #200)
- Word Ladder → Teaches: BFS on graph (word transformation) Difficulty: ★★★★★ (LeetCode #127)
- Course Schedule (detect cycle + topological sort) → Teaches: DFS/BFS cycle detection + topological order Difficulty: ★★★★★ (LeetCode #207)
Tips – How to practice these examples effectively
- First solve without looking at solution (even if it takes 1–2 hours)
- Write clean code — good variable names, comments
- Always analyze:
- Time complexity (best / average / worst)
- Space complexity
- Can we optimize space? (very common follow-up)
- Dry run on paper with 4–5 small test cases
- Think of edge cases: empty array, single element, all same elements, already sorted, reverse sorted, duplicates, etc.
- After solving → read official solution / discuss on LeetCode/Reddit
Would you like me to:
- Give you 50–70 most important problems categorized by topic
- Explain any one specific problem from the list above in full detail (with code + dry run)
- Make a personalized practice plan (beginner / intermediate / advanced)
- Focus on one topic (arrays, DP, graphs, binary search, etc.) with 5–8 strong problems
Just tell me what you want next — I’ll continue in the same detailed, teacher-like style 😊
