Dev Skills
Go·Section 3 of 15 · 16 lessons · about 15 hours

DSA in Go (Golang): coding interview problems, solved

How to read judge input fast, then fifteen problems campus hiring tests and online assessments ask most (maps, slices, linked lists, trees, recursion, DP, graphs, sliding windows), each solved idiomatically in Go with the trap that costs marks.

  1. 01Fast input and output for online judges
    How to read the input HackerRank, CodeChef and company OAs send on stdin, and print the answer fast enough to pass the time limit.
    Graded challenge
  2. 02Two sum with a map
    The most-asked warm-up in any coding round, and the cleanest demonstration of why a map beats a nested loop.
    Graded challenge
  3. 03Reverse and rotate a slice in place
    Two pointers, the three-reversal trick, and the slice aliasing that makes "in place" mean what it says.
    Graded challenge
  4. 04Frequency count and top K
    Count with a map, rank with a sort or a heap, and never let map order leak into your answer.
    Graded challenge
  5. 05Valid parentheses with a slice as a stack
    Go has no stack type, because a slice already is one. Push with append, pop by re-slicing.
    Graded challenge
  6. 06Binary search, by hand and from the stdlib
    Write it once without an off-by-one, then know when `slices.BinarySearch` and `sort.Search` do it for you.
    Graded challenge
  7. 07LRU cache with container/list and a map
    The design question disguised as a coding question: two structures, each covering what the other cannot.
    Graded challenge
  8. 08Reverse a linked list
    Three pointers and one saved link. The question that checks you can move pointers without losing the rest of the list.
    Graded challenge
  9. 09Detect a cycle with two pointers
    Floyd's tortoise and hare: find a loop in a linked list with no extra memory.
    Graded challenge
  10. 10Merge two sorted lists
    A dummy head node removes every special case, and the leftover tail is attached in one line.
    Graded challenge
  11. 11Binary tree depth and level order
    Recursion for depth, a slice as a queue for level order. The two shapes every tree question uses.
    Graded challenge
  12. 12Subsets and permutations with backtracking
    Choose, recurse, undo. And the one-line copy that stops every answer turning into the same slice.
    Graded challenge
  13. 13Dynamic programming: stairs, coins, LCS
    Write the answer for a small case in terms of smaller ones, then fill a table from the bottom up.
    Graded challenge
  14. 14Number of islands: DFS on a grid
    A grid is a graph in disguise. Find unvisited land, flood it, count one.
    Graded challenge
  15. 15Merge overlapping intervals
    Sort by start, then one pass. The sort does the hard part.
    Graded challenge
  16. 16Longest substring without repeating characters
    A window with two edges and a map of where each character was last seen. Rune-aware, so it counts characters, not bytes.
    Graded challenge

What each lesson covers

By the end of this section you will be able to do everything below. Each lesson has runnable Go examples you can edit and run in the browser.

  1. 1. Fast input and output for online judges

    • Read T test cases of "n, then n numbers" with bufio.NewReader and fmt.Fscan.
    • Print answers through bufio.NewWriter and explain why defer Flush() is needed.
    • Read very long lines with bufio.Scanner and Buffer, and words with ScanWords.

    Remember: Buffered in, buffered out, and Flush before you leave.

  2. 2. Two sum with a map

    • Write a one-pass two sum with a map and state its O(n) time and space.
    • Explain why comma-ok is needed when 0 is a valid index.
    • Predict what goes wrong if you insert before you look up.

    Remember: When the question is "have I seen X before?", the answer is a map.

  3. 3. Reverse and rotate a slice in place

    • Write in-place reverse and rotate with two pointers and three reversals.
    • Predict how reversing a sub-slice changes the original slice.
    • Fix the divide-by-zero panic on an empty slice.

    Remember: A slice header is three words. Copying it never copies the elements.

  4. 4. Frequency count and top K

    • Write a top-k word counter with a map and slices.SortFunc.
    • Explain why a tie-break is needed for deterministic output.
    • Predict the output order when two words tie on count.

    Remember: If the output depends on map order, it is a bug that only fails sometimes.

  5. 5. Valid parentheses with a slice as a stack

    • Write a bracket checker that uses a slice as a stack.
    • Fix the out-of-range panic when a closer arrives on an empty stack.
    • Explain why counting brackets alone is not enough.

    Remember: Any "matching pairs, most recent first" problem is a stack.

  6. 6. Binary search, by hand and from the stdlib

    • Write an off-by-one-free binary search over an inclusive range.
    • Predict the lo, hi and mid values at each step for a small input.
    • Explain when sort.Search returns n and how to guard it.

    Remember: Binary search works on any monotonic yes/no question, not only on sorted slices.

  7. 7. LRU cache with container/list and a map

    • Write an LRU cache with O(1) Get and Put using a map and container/list.
    • Explain why the list entry must store its key.
    • Predict which key is evicted after a sequence of calls.

    Remember: When one data structure cannot do both jobs in O(1), use two that point at each other.

  8. 8. Reverse a linked list

    • Write an iterative linked-list reversal with O(1) extra space.
    • Predict prev, cur and next at each step of a dry run.
    • Fix the bug of overwriting Next before saving it.

    Remember: Draw three boxes and the arrows before you write a line. Pointer bugs are drawing bugs.

  9. 9. Detect a cycle with two pointers

    • Write Floyd's cycle check with slow and fast pointers.
    • Explain why both fast and fast.Next must be checked for nil.
    • Predict where slow and fast meet on a small cyclic list.

    Remember: Two runners on a circular track always meet. On a straight one, the fast one just finishes.

  10. 10. Merge two sorted lists

    • Write a merge of two sorted lists using a dummy head.
    • Explain why <= keeps the merge stable.
    • Predict the result when one list is nil.

    Remember: When the first item of a result is a special case, add a dummy in front of it.

  11. 11. Binary tree depth and level order

    • Write maxDepth with recursion and level order with a slice queue.
    • Explain why len(queue) is read before each level's loop.
    • Predict the queue contents after each round of BFS.

    Remember: Depth-first is a stack (or recursion). Breadth-first is a queue. Most tree problems are one of the two.

  12. 12. Subsets and permutations with backtracking

    • Write subsets and permutations with choose, recurse, undo.
    • Fix the bug where every saved answer is the same slice.
    • Explain why the output size sets the complexity.

    Remember: If every answer in your output looks the same, you saved the slice, not a copy of it.

  13. 13. Dynamic programming: stairs, coins, LCS

    • Write the recurrence for stairs, coin change and LCS before coding.
    • Fill a small DP table by hand and check it against the program.
    • Explain why plain recursion is exponential and DP is not.

    Remember: If plain recursion recomputes the same small cases again and again, store them. That is all DP is.

  14. 14. Number of islands: DFS on a grid

    • Write a flood fill that counts islands in a grid.
    • Fix out-of-range panics by bounds-checking before indexing.
    • Predict the island count for a small grid.

    Remember: Any "count the connected groups" question is a flood fill.

  15. 15. Merge overlapping intervals

    • Write interval merging with one sort and one pass.
    • Explain why updating a copy of the last interval does nothing.
    • Predict the merged output for a small input.

    Remember: Most interval problems become easy once they are sorted by start.

  16. 16. Longest substring without repeating characters

    • Write a rune-aware sliding window for the longest unique substring.
    • Explain why the j >= start check is needed.
    • Predict start and best at each step for a small string.

    Remember: If the question says "longest/shortest contiguous ... such that", think sliding window.

Test yourself