AlgoDaily

Intermediate Python for Google Interviews

Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.

Course Kickoff

Why this lesson matters

Welcome to the first lesson of your 90 Days Python course for Google-style interview prep.

Because you code part-time, our first goal is not to memorize every Python feature. It is to build a reliable interview toolkit: the Python habits that help you turn an idea into clean code under pressure.

In this lesson, we will focus on intermediate Python skills that repeatedly show up in coding interviews:

  • Choosing the right built-in data structure
  • Looping clearly with enumerate, zip, and comprehensions
  • Using dictionaries and sets for fast lookups
  • Avoiding mutability surprises
  • Writing functions that are easy to test and explain

Later lessons will go deeper into algorithms, system design, optimization, and mock interviews. Today is about making Python feel like a sharp, dependable tool.

Course Kickoff

Core Python Tools

The interview mindset

In a Google-style coding interview, Python is not judged by how many features you know. It is judged by whether your code is:

  • Correct — handles normal and edge cases
  • Readable — easy for the interviewer to follow
  • Efficient enough — avoids unnecessary work
  • Explainable — you can narrate your choices

A useful mental model is:

Problem statement
      ↓
Choose representation
      ↓
Write simple logic
      ↓
Test with examples
      ↓
Discuss complexity

Intermediate Python helps most in the middle: choosing representation and writing simple logic.

For example:

  • Need ordered items? Use a list.
  • Need unique membership checks? Use a set.
  • Need mapping from key to value? Use a dict.
  • Need an immutable pair or coordinate? Use a tuple.

This sounds basic, but many interview solutions become hard because the wrong structure was chosen early.

Core Python Tools

Choose Structure

You are solving an interview problem and need to repeatedly check whether a value has already appeared before.

Which Python data structure is usually the best first choice?

Loop Clearly

Prefer readable iteration

Interview code should reduce mental overhead. Python gives you tools to avoid manual indexing when possible.

Use:

  • enumerate(items) when you need both index and value
  • zip(a, b) when walking two sequences together
  • comprehensions for simple transformations

The goal is not to be clever. The goal is to make the shape of your logic obvious.

```python
# Standalone example: clear iteration patterns

names = ["Ada", "Grace", "Linus"]
scores = [95, 98, 91]

print("Indexed names:")
for i, name in enumerate(names):
    print(i, name)

print("\nName-score pairs:")
for name, score in zip(names, scores):
    print(f"{name}: {score}")

passed = [name for name, score in zip(names, scores) if score >= 95]
print("\nPassed:", passed)
```

Enumerate Habit

True or false?

When you need both an index and a value while looping, enumerate is usually clearer than manually updating an index variable.

Hashing Patterns

Dictionaries and sets are interview workhorses

Many beginner solutions scan the same list again and again. Intermediate Python lets you avoid repeated scanning by remembering useful information.

Two common patterns:

1. set for seen values

Use this when you only care whether something has appeared.

2. dict for value → information

Use this when you need to attach data to a key, such as an index, count, or previous state.

This is the foundation for many problems you will later solve with more formal algorithms.

```python
# Standalone example: using dict and set for fast lookups

def first_duplicate(nums):
    seen = set()
    for x in nums:
        if x in seen:
            return x
        seen.add(x)
    return None


def two_sum_indices(nums, target):
    index_by_value = {}
    for i, x in enumerate(nums):
        need = target - x
        if need in index_by_value:
            return [index_by_value[need], i]
        index_by_value[x] = i
    return []

print(first_duplicate([4, 2, 7, 2, 9]))
print(two_sum_indices([3, 5, 1, 7], 8))
```

Dictionary Default

Fill in the blank.

To safely read a dictionary value while providing a fallback for a missing key, use the dictionary method named ____.

Mutability Matters

Avoid hidden state bugs

Python variables often refer to objects. Some objects are mutable, meaning they can be changed in place.

Common mutable types:

  • list
  • dict
  • set

Common immutable types:

  • int
  • str
  • tuple

Why this matters in interviews:

  • Mutating a list inside a helper function can affect the caller.
  • Reusing the same list accidentally can create hard-to-find bugs.
  • Default arguments like an empty list can persist between calls.

A safe habit: use None as the default, then create the list inside the function.

```python
# Standalone example: safe handling of a mutable default

def add_task(task, tasks=None):
    if tasks is None:
        tasks = []
    tasks.append(task)
    return tasks

first = add_task("review arrays")
second = add_task("practice hashing")
combined = add_task("mock question", first)

print("first:", first)
print("second:", second)
print("combined:", combined)
```

Sorting Keys

You need to sort words by shortest length first. If two words have the same length, sort them alphabetically.

Which sorting strategy best matches that requirement?

Practice Routine

Your interview coding loop

For the next few weeks, practice each Python problem with the same rhythm:

  1. Restate the problem in plain English.
  2. Pick the data structure before coding.
  3. Write a small helper if it clarifies the logic.
  4. Use Pythonic loops like enumerate and zip when appropriate.
  5. Test tiny cases out loud.
  6. Explain time and space complexity simply.

A strong interview solution is not just code that works. It is code you can guide someone through.

In the next lesson, Advanced Algorithms and Data Structures, we will use this Python toolkit as the base for more demanding problem patterns.

Practice Routine

Open interactive view →