Data structures organize values; algorithms describe the steps used to solve a problem. In Python, start with lists, dictionaries, sets and tuples, then learn searching, sorting, stacks, queues and recursion. Practise explaining why a solution works and how its time and memory requirements grow.
This beginner guide assumes you can write Python functions, loops and conditions. If those are unfamiliar, build that foundation through Python programming practice before tackling timed interview questions.
Choose a structure based on the operation
- List: an ordered, mutable sequence. Indexing is constant time; searching for an arbitrary value is linear time. Appending is amortized constant time, while inserting or removing at the front shifts elements.
- Tuple: an ordered, immutable sequence. Use it for fixed groups of values; immutability does not make every object stored inside a tuple immutable.
- Dictionary: a mapping from unique hashable keys to values. Lookup and insertion are average constant time, with worse cases possible.
- Set: a collection of unique hashable values. It is useful for membership checks and removing duplicates when you do not need sequence order.
Before writing code, identify your most frequent operation. A list is convenient for preserving order, while a dictionary can make repeated lookups by identifier simpler. Do not choose a more complicated structure just because it appears in an interview syllabus.
Understand time complexity with linear search
Big O describes how resource use grows as input size increases. It does not predict exact execution time. A linear search checks values one at a time, so its worst-case time is O(n), where n is the number of elements. Its extra space is O(1).
def linear_search(values, target):
for index, value in enumerate(values):
if value == target:
return index
return -1
assert linear_search([8, 3, 5], 3) == 1
assert linear_search([], 3) == -1
assert linear_search([8, 3, 5], 9) == -1
Explain the empty-list case, the missing-value case and what happens with duplicate values. This version returns the first matching index. A different requirement, such as returning all matches, changes the output and memory needs.
Use binary search only when its assumptions hold
Binary search repeatedly halves a sorted search range. On an indexable sorted sequence, the search takes O(log n) time and the iterative implementation uses O(1) extra space. If you first need to sort an unsorted list, account for that preparation cost and any change to the original ordering.
def binary_search(values, target):
left, right = 0, len(values) - 1
while left <= right:
middle = (left + right) // 2
if values[middle] == target:
return middle
if values[middle] < target:
left = middle + 1
else:
right = middle - 1
return -1
assert binary_search([2, 4, 7, 9], 7) == 2
assert binary_search([], 7) == -1
assert binary_search([2, 4, 7, 9], 3) == -1
The input must already be in ascending order for this implementation. With duplicates it can return any matching index, so finding the first match requires additional logic. Test one-element lists and values outside the list's range.
Practise stacks and queues with clear examples
A stack follows last in, first out. A list with append and pop is suitable for a simple stack, such as a history of actions that can be undone. Check whether the stack is empty before removing an element.
A queue follows first in, first out. Use collections.deque with append and popleft for a queue of pending tasks. Repeatedly removing the first element of a Python list shifts the remaining items, which makes that operation inefficient for a large queue.
Use dictionaries for frequency counting
Counting values is a good first exercise in replacing nested loops with a mapping. The example below makes one pass through the input. Under average dictionary-operation assumptions, it takes O(n) time and O(k) extra space for k distinct values.
def frequencies(values):
counts = {}
for value in values:
counts[value] = counts.get(value, 0) + 1
return counts
assert frequencies(["a", "b", "a"]) == {"a": 2, "b": 1}
assert frequencies([]) == {}
Dictionary keys must be hashable. Strings and integers are common choices; a mutable list cannot be a dictionary key. For text exercises, decide whether case, punctuation and whitespace should affect the counts before implementing the solution.
A practical DSA learning sequence
- Solve list and string tasks: minimum, maximum, reversal, filtering and duplicate detection.
- Use sets and dictionaries for membership, frequency counts and grouping.
- Implement linear and binary search and explain their assumptions.
- Study simple sorting algorithms to understand comparisons and movement, then learn when to use Python's built-in sorting.
- Practise stacks, queues and recursion, including a base case and progress toward it.
- Move to linked lists, trees, graphs and dynamic programming once you can trace simpler solutions confidently.
How to review each solution
Write the problem in your own words and create a small example before coding. State assumptions, walk through the algorithm, then test empty inputs, duplicates, missing values and boundary cases. Calculate time and extra-space complexity separately. Finally, compare an alternative approach and explain the tradeoff.
Keep a practice log of mistakes such as off-by-one errors or incorrect stopping conditions. Re-solve a problem later without looking at the answer. Understanding a small set of patterns is more useful than memorizing a large collection of copied solutions.
Further practice
Use the official Python data structures tutorial and deque documentation as references. For a structured starting point, explore Python programming at Forsk and the current course catalogue.
