Common Interview Challenges

A curated collection of the most common algorithm interview problems with optimal Go solutions.

Array Problems

1. Two Sum

Problem: Find two numbers that add up to target.

 1func TwoSum(nums []int, target int) []int {
 2    seen := make(map[int]int)
 3    
 4    for i, num := range nums {
 5        complement := target - num
 6        if j, exists := seen[complement]; exists {
 7            return []int{j, i}
 8        }
 9        seen[num] = i
10    }
11    
12    return nil
13}

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

2. Best Time to Buy and Sell Stock

Problem: Maximize profit from one buy and one sell.

 1func MaxProfit(prices []int) int {
 2    minPrice := prices[0]
 3    maxProfit := 0
 4    
 5    for _, price := range prices {
 6        if price < minPrice {
 7            minPrice = price
 8        } else if price - minPrice > maxProfit {
 9            maxProfit = price - minPrice
10        }
11    }
12    
13    return maxProfit
14}

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

3. Container With Most Water

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

 1func MaxArea(height []int) int {
 2    left, right := 0, len(height)-1
 3    maxArea := 0
 4    
 5    for left < right {
 6        width := right - left
 7        h := min(height[left], height[right])
 8        area := width * h
 9        
10        if area > maxArea {
11            maxArea = area
12        }
13        
14        // Move pointer with smaller height
15        if height[left] < height[right] {
16            left++
17        } else {
18            right--
19        }
20    }
21    
22    return maxArea
23}

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

String Problems

4. Longest Substring Without Repeating Characters

Problem: Find length of longest substring without repeating characters.

 1func LengthOfLongestSubstring(s string) int {
 2    charMap := make(map[byte]int)
 3    left, maxLen := 0, 0
 4    
 5    for right := 0; right < len(s); right++ {
 6        if idx, exists := charMap[s[right]]; exists && idx >= left {
 7            left = idx + 1
 8        }
 9        
10        charMap[s[right]] = right
11        if right - left + 1 > maxLen {
12            maxLen = right - left + 1
13        }
14    }
15    
16    return maxLen
17}

Time: $O(n)$, Space: $O(min(n, m))$ where m is charset size

5. Valid Parentheses

Problem: Check if string has valid parentheses.

 1func IsValid(s string) bool {
 2    stack := []rune{}
 3    pairs := map[rune]rune{')': '(', '}': '{', ']': '['}
 4    
 5    for _, char := range s {
 6        if char == '(' || char == '{' || char == '[' {
 7            stack = append(stack, char)
 8        } else {
 9            if len(stack) == 0 || stack[len(stack)-1] != pairs[char] {
10                return false
11            }
12            stack = stack[:len(stack)-1]
13        }
14    }
15    
16    return len(stack) == 0
17}

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

Linked List Problems

6. Merge Two Sorted Lists

Problem: Merge two sorted linked lists.

 1func MergeTwoLists(l1, l2 *ListNode) *ListNode {
 2    dummy := &ListNode{}
 3    curr := dummy
 4    
 5    for l1 != nil && l2 != nil {
 6        if l1.Val <= l2.Val {
 7            curr.Next = l1
 8            l1 = l1.Next
 9        } else {
10            curr.Next = l2
11            l2 = l2.Next
12        }
13        curr = curr.Next
14    }
15    
16    if l1 != nil {
17        curr.Next = l1
18    } else {
19        curr.Next = l2
20    }
21    
22    return dummy.Next
23}

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

7. Reverse Linked List

Problem: Reverse a linked list.

 1func ReverseList(head *ListNode) *ListNode {
 2    var prev *ListNode
 3    curr := head
 4    
 5    for curr != nil {
 6        next := curr.Next
 7        curr.Next = prev
 8        prev = curr
 9        curr = next
10    }
11    
12    return prev
13}

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

Tree Problems

8. Maximum Depth of Binary Tree

Problem: Find maximum depth of binary tree.

 1func MaxDepth(root *TreeNode) int {
 2    if root == nil {
 3        return 0
 4    }
 5    
 6    leftDepth := MaxDepth(root.Left)
 7    rightDepth := MaxDepth(root.Right)
 8    
 9    return 1 + max(leftDepth, rightDepth)
10}

Time: $O(n)$, Space: $O(h)$ where h is height

9. Validate Binary Search Tree

Problem: Check if tree is valid BST.

 1func IsValidBST(root *TreeNode) bool {
 2    return validate(root, nil, nil)
 3}
 4
 5func validate(node *TreeNode, min, max *int) bool {
 6    if node == nil {
 7        return true
 8    }
 9    
10    if (min != nil && node.Val <= *min) || (max != nil && node.Val >= *max) {
11        return false
12    }
13    
14    return validate(node.Left, min, &node.Val) && 
15           validate(node.Right, &node.Val, max)
16}

Time: $O(n)$, Space: $O(h)$

10. Lowest Common Ancestor

Problem: Find LCA of two nodes in BST.

 1func LowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
 2    if root == nil {
 3        return nil
 4    }
 5    
 6    // If both nodes are in left subtree
 7    if p.Val < root.Val && q.Val < root.Val {
 8        return LowestCommonAncestor(root.Left, p, q)
 9    }
10    
11    // If both nodes are in right subtree
12    if p.Val > root.Val && q.Val > root.Val {
13        return LowestCommonAncestor(root.Right, p, q)
14    }
15    
16    // Current node is LCA
17    return root
18}

Time: $O(h)$, Space: $O(h)$

Dynamic Programming

11. Climbing Stairs

Problem: How many ways to climb n stairs (1 or 2 steps at a time).

 1func ClimbStairs(n int) int {
 2    if n <= 2 {
 3        return n
 4    }
 5    
 6    prev2, prev1 := 1, 2
 7    
 8    for i := 3; i <= n; i++ {
 9        curr := prev1 + prev2
10        prev2 = prev1
11        prev1 = curr
12    }
13    
14    return prev1
15}

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

12. House Robber

Problem: Maximize money robbed from non-adjacent houses.

 1func Rob(nums []int) int {
 2    if len(nums) == 0 {
 3        return 0
 4    }
 5    if len(nums) == 1 {
 6        return nums[0]
 7    }
 8    
 9    prev2, prev1 := 0, nums[0]
10    
11    for i := 1; i < len(nums); i++ {
12        curr := max(prev1, prev2 + nums[i])
13        prev2 = prev1
14        prev1 = curr
15    }
16    
17    return prev1
18}

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

13. Coin Change

Problem: Minimum coins needed to make amount.

 1func CoinChange(coins []int, amount int) int {
 2    dp := make([]int, amount+1)
 3    for i := 1; i <= amount; i++ {
 4        dp[i] = amount + 1 // Infinity
 5    }
 6    dp[0] = 0
 7    
 8    for i := 1; i <= amount; i++ {
 9        for _, coin := range coins {
10            if coin <= i {
11                dp[i] = min(dp[i], dp[i-coin] + 1)
12            }
13        }
14    }
15    
16    if dp[amount] > amount {
17        return -1
18    }
19    return dp[amount]
20}

Time: $O(n \times amount)$, Space: $O(amount)$

Graph Problems

14. Number of Islands

Problem: Count number of islands in 2D grid.

 1func NumIslands(grid [][]byte) int {
 2    if len(grid) == 0 {
 3        return 0
 4    }
 5    
 6    count := 0
 7    for i := 0; i < len(grid); i++ {
 8        for j := 0; j < len(grid[0]); j++ {
 9            if grid[i][j] == '1' {
10                dfs(grid, i, j)
11                count++
12            }
13        }
14    }
15    
16    return count
17}
18
19func dfs(grid [][]byte, i, j int) {
20    if i < 0 || i >= len(grid) || j < 0 || j >= len(grid[0]) || grid[i][j] != '1' {
21        return
22    }
23    
24    grid[i][j] = '0' // Mark as visited
25    
26    dfs(grid, i+1, j)
27    dfs(grid, i-1, j)
28    dfs(grid, i, j+1)
29    dfs(grid, i, j-1)
30}

Time: $O(m \times n)$, Space: $O(m \times n)$ for recursion

15. Course Schedule (Cycle Detection)

Problem: Can you finish all courses given prerequisites?

 1func CanFinish(numCourses int, prerequisites [][]int) bool {
 2    graph := make([][]int, numCourses)
 3    for _, pre := range prerequisites {
 4        graph[pre[1]] = append(graph[pre[1]], pre[0])
 5    }
 6    
 7    visited := make([]int, numCourses) // 0: unvisited, 1: visiting, 2: visited
 8    
 9    var hasCycle func(int) bool
10    hasCycle = func(course int) bool {
11        if visited[course] == 1 {
12            return true // Cycle detected
13        }
14        if visited[course] == 2 {
15            return false // Already processed
16        }
17        
18        visited[course] = 1 // Mark as visiting
19        
20        for _, next := range graph[course] {
21            if hasCycle(next) {
22                return true
23            }
24        }
25        
26        visited[course] = 2 // Mark as visited
27        return false
28    }
29    
30    for i := 0; i < numCourses; i++ {
31        if hasCycle(i) {
32            return false
33        }
34    }
35    
36    return true
37}

Time: $O(V + E)$, Space: $O(V + E)$

Backtracking

16. Generate Parentheses

Problem: Generate all valid combinations of n pairs of parentheses.

 1func GenerateParenthesis(n int) []string {
 2    result := []string{}
 3    backtrack(&result, "", 0, 0, n)
 4    return result
 5}
 6
 7func backtrack(result *[]string, current string, open, close, max int) {
 8    if len(current) == max*2 {
 9        *result = append(*result, current)
10        return
11    }
12    
13    if open < max {
14        backtrack(result, current+"(", open+1, close, max)
15    }
16    if close < open {
17        backtrack(result, current+")", open, close+1, max)
18    }
19}

Time: $O(\frac{4^n}{\sqrt{n}})$ (Catalan number), Space: $O(n)$

17. Permutations

Problem: Generate all permutations of array.

 1func Permute(nums []int) [][]int {
 2    result := [][]int{}
 3    backtrackPermute(&result, nums, 0)
 4    return result
 5}
 6
 7func backtrackPermute(result *[][]int, nums []int, start int) {
 8    if start == len(nums) {
 9        perm := make([]int, len(nums))
10        copy(perm, nums)
11        *result = append(*result, perm)
12        return
13    }
14    
15    for i := start; i < len(nums); i++ {
16        nums[start], nums[i] = nums[i], nums[start]
17        backtrackPermute(result, nums, start+1)
18        nums[start], nums[i] = nums[i], nums[start] // Backtrack
19    }
20}

Time: $O(n!)$, Space: $O(n)$

Bit Manipulation

18. Single Number

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

1func SingleNumber(nums []int) int {
2    result := 0
3    for _, num := range nums {
4        result ^= num // XOR cancels out duplicates
5    }
6    return result
7}

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

19. Number of 1 Bits

Problem: Count number of 1 bits in integer.

1func HammingWeight(num uint32) int {
2    count := 0
3    for num != 0 {
4        count++
5        num &= (num - 1) // Remove rightmost 1 bit
6    }
7    return count
8}

Time: $O(\log n)$, Space: $O(1)$

Sliding Window

20. Minimum Window Substring

Problem: Find minimum window in s that contains all characters of t.

 1func MinWindow(s string, t string) string {
 2    if len(s) < len(t) {
 3        return ""
 4    }
 5    
 6    need := make(map[byte]int)
 7    for i := 0; i < len(t); i++ {
 8        need[t[i]]++
 9    }
10    
11    left, right := 0, 0
12    valid := 0
13    start, length := 0, len(s)+1
14    window := make(map[byte]int)
15    
16    for right < len(s) {
17        c := s[right]
18        right++
19        
20        if _, exists := need[c]; exists {
21            window[c]++
22            if window[c] == need[c] {
23                valid++
24            }
25        }
26        
27        for valid == len(need) {
28            if right - left < length {
29                start = left
30                length = right - left
31            }
32            
33            d := s[left]
34            left++
35            
36            if _, exists := need[d]; exists {
37                if window[d] == need[d] {
38                    valid--
39                }
40                window[d]--
41            }
42        }
43    }
44    
45    if length == len(s)+1 {
46        return ""
47    }
48    return s[start : start+length]
49}

Time: $O(m + n)$, Space: $O(m + n)$

Helper Functions

 1func min(a, b int) int {
 2    if a < b {
 3        return a
 4    }
 5    return b
 6}
 7
 8func max(a, b int) int {
 9    if a > b {
10        return a
11    }
12    return b
13}

Problem Categories

Easy (Good for Warm-up)

  • Two Sum
  • Valid Parentheses
  • Merge Two Sorted Lists
  • Maximum Depth of Binary Tree
  • Climbing Stairs
  • Single Number

Medium (Core Interview Questions)

  • Longest Substring Without Repeating
  • Container With Most Water
  • Reverse Linked List
  • Validate BST
  • Coin Change
  • Number of Islands
  • Generate Parentheses

Hard (Advanced)

  • Minimum Window Substring
  • Merge K Sorted Lists
  • Median of Two Sorted Arrays
  • Trapping Rain Water

Study Strategy

  1. Master the patterns: Two pointers, sliding window, DFS/BFS, DP
  2. Practice daily: 1-2 problems per day
  3. Time yourself: 30-45 minutes per problem
  4. Understand, don't memorize: Focus on why solutions work
  5. Review mistakes: Keep a log of problems you struggled with

Related Snippets