./basic --list

Basic Questions — 1–5 years

Expand any question for an animated debug trace, concise solution, and source code.

Quick reference — expand for debug steps & solution
  • 01
    Two SumLeetCodeEasy
    Use a hashmap storing value→index for O(n) time and O(n) space.
  • 02
    Contains DuplicateLeetCodeEasy
    Use a HashSet to check duplicates in O(n) time.
  • 03
    Best Time to Buy and Sell StockLeetCodeEasy
    Single pass tracking min price and max profit.
  • 04
    Merge IntervalsLeetCodeMedium
    Sort by start and merge adjacent overlapping intervals in one pass.
  • 05
    Product of Array Except SelfLeetCodeMedium
    Two-pass approach: left product and right product without using division.
  • 06
    Longest Palindromic SubstringLeetCodeMedium
    Expand-around-center yields O(n^2) worst-case but is simple and fast in practice.
  • 07
    Valid ParenthesesLeetCodeEasy
    Use a stack that stores expected closing bracket for O(n) time.
  • 08
    Group AnagramsLeetCodeMedium
    Use character count or sorted string as key to group anagrams.
  • 09
    Top K Frequent ElementsLeetCodeMedium
    Count frequencies and use a heap or bucket sort for O(n log k).
  • 10
    Rotate ArrayLeetCodeEasy
    Reverse segments to rotate in-place in O(n) time and O(1) space.
  • 11
    Subarray Sum Equals KLeetCodeMedium
    Prefix-sum with hashmap for O(n) time counting subarrays summing to k.
  • 12
    Find pivot where order breaks using modified binary search in O(log n).
  • 13
    Valid AnagramLeetCodeEasy
    Use fixed-size frequency array for lowercase letters to check equality.
  • 14
    Intersection of Two Arrays IILeetCodeEasy
    Use hashmap counts or sort both arrays and two-pointer merge-like sweep.
  • 15
    Merge Two Sorted ListsLeetCodeEasy
    Iterative merge using dummy head yields O(n) time and O(1) extra space.
  • 16
    Number of IslandsLeetCodeMedium
    Flood-fill each island via DFS/BFS and count them; O(mn) time.
  • 17
    LRU CacheLeetCodeMedium
    Use LinkedHashMap or custom doubly-linked list + hashmap for O(1) get/put.
  • 18
    Median of Two Sorted ArraysLeetCodeHard
    Binary search on the partition index of the smaller array in O(log min(n,m)) time.
  • 19
    Word BreakLeetCodeMedium
    DP fill with boolean states using dictionary lookup; O(n^2) time worst-case.
  • 20
    Rotate ImageLeetCodeMedium
    Transpose and reverse rows in-place in O(n^2) time.
  • 21
    Kth Smallest Element in a BSTLeetCodeMedium
    Inorder traversal with counter to stop at k; O(h + k) time.
  • 22
    Find All Duplicates in an ArrayLeetCodeMedium
    Mark visited positions by negation to achieve O(n) time and O(1) extra space.
  • 23
    Find Duplicate NumberLeetCodeHard
    Floyd Tortoise and Hare finds the cycle entry representing the duplicate in O(n) time and O(1) space.
  • 24
    Minimum Window SubstringLeetCodeHard
    Sliding window with frequency counts and a matched-character counter.
  • 25
    Evaluate Reverse Polish NotationLeetCodeMedium
    Stack-based evaluation of a postfix expression.
  • 26
    Valid SudokuLeetCodeMedium
    Use sets keyed by row/col/box and detect duplicates in O(1) checks.
  • 27
    Implement Trie (Prefix Tree)LeetCodeMedium
    Trie with a children array and a boolean word flag supports insert/search/prefix in O(length) time.
  • 28
    Rotate Linked ListLeetCodeMedium
    Connect tail to head, find the new tail after len - k%len steps, then cut.
  • 29
    Course ScheduleLeetCodeMedium
    Detect a valid topological ordering via indegree counting and BFS (Kahn's algorithm).
  • 30
    Sliding Window MaximumLeetCodeHard
    Use a monotonic deque of indices to get O(n) time sliding-window max.
  • 31
    Maximum SubarrayLeetCodeMedium
    Kadane's algorithm: extend the current subarray or start fresh, whichever is larger.
  • 32
    Climbing StairsLeetCodeEasy
    Fibonacci-style DP: ways(n) = ways(n-1) + ways(n-2).
  • 33
    Reverse Linked ListLeetCodeEasy
    Iteratively flip each next pointer while walking the list once.
  • 34
    Linked List CycleLeetCodeEasy
    Floyd cycle detection: fast pointer meets slow pointer if a cycle exists.
  • 35
    Invert Binary TreeLeetCodeEasy
    Recursively swap left and right children at every node.
  • 36
    Maximum Depth of Binary TreeLeetCodeEasy
    Recursive DFS: depth = 1 + max(depth(left), depth(right)).
  • 37
    Same TreeLeetCodeEasy
    Recursively compare node values and structure of both trees.
  • 38
    Search in Rotated Sorted ArrayLeetCodeMedium
    Modified binary search: figure out which half is sorted, then decide which side to keep.
  • 39
    3SumLeetCodeMedium
    Sort, fix one element, then use two pointers to find pairs summing to its negation.
  • 40
    Container With Most WaterLeetCodeMedium
    Two pointers from both ends; always move the shorter wall inward.
  • 41
    Trapping Rain WaterLeetCodeHard
    Two pointers tracking left/right max walls; water at each cell is bounded by the smaller max.
  • 42
    Sliding window with a last-seen-index map to jump the left pointer past repeats.
  • 43
    Longest Common SubsequenceLeetCodeMedium
    2D DP: dp[i][j] = dp[i-1][j-1]+1 on a match, else max of dropping a char from either string.
  • 44
    Coin ChangeLeetCodeMedium
    Bottom-up DP over amounts: dp[a] = min coins to make amount a.
  • 45
    House RobberLeetCodeMedium
    DP: at each house, pick max of skipping it vs robbing it plus the best two houses back.
  • 46
    Single NumberLeetCodeEasy
    XOR every element; duplicates cancel out, leaving the single number.
  • 47
    Missing NumberLeetCodeEasy
    XOR indices 0..n with all values; everything present cancels, leaving the missing number.
  • 48
    Move ZeroesLeetCodeEasy
    Two pointers: swap each non-zero found into the next available front slot.
  • 49
    Valid PalindromeLeetCodeEasy
    Clean the string to lowercase alphanumerics, then two-pointer compare from both ends.
  • 50
    Reverse IntegerLeetCodeMedium
    Pop digits from the end and build the reversed number, checking for 32-bit overflow.
  • 51
    FizzBuzzLeetCodeEasy
    Classic screening question: check divisibility by 15, then 3, then 5.
  • 52
    Merge Sorted ArrayLeetCodeEasy
    Merge from the back of nums1 so existing values are never overwritten prematurely.
  • 53
    Slow/fast pointers: only advance slow (and copy) when a new value is found.
  • 54
    Majority ElementLeetCodeEasy
    Boyer-Moore voting: track a candidate and a running count, swapping candidate when count hits 0.
  • 55
    Set Matrix ZeroesLeetCodeMedium
    Record which rows/columns contain a zero, then zero them out in a second pass.
Questions curated for 1–5 years experience. Solutions are concise; adapt for production.