Complexity basics
Count how work grows with input size. One loop is commonly linear, or O(n), which means the amount of work usually grows in step with the size of the input.
The goal here is not to memorize symbols but to start noticing the shape of a solution. A method that checks every item can be fine for small inputs, while a method that cuts the search space in half can become valuable when the input gets large.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.Two sum
Use a dictionary to remember numbers seen so far and find a complement in one pass. This is a classic example of trading a little memory for a much faster lookup.
The key idea is to remember what you have already seen instead of starting over each time. That way, the moment the matching number appears, the answer is already waiting in the dictionary.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.Palindrome check
Two pointers can compare a sequence from both ends. This avoids checking the same positions more than necessary and gives you a pattern that appears again in many interview problems.
The useful mental picture here is symmetry. If the left and right sides match as you move inward, the string passes the test; if not, you can stop immediately.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.Valid parentheses
A stack tracks brackets that still need a match. The newest opening bracket has to be matched first, which is why a stack fits this problem so neatly.
Try to read the code as a small bookkeeping system. Each opener is stored, and each closer asks whether the most recent opener is the correct partner.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.Binary search
Binary search repeatedly cuts a sorted search range in half. It works by using the middle value to rule out everything on one side of the list.
This pattern is useful because the amount of work drops quickly as the input grows. The catch is that the data must already be sorted, or the middle value does not tell you anything reliable.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.FizzBuzz challenge
Practice condition ordering: test the most specific case first. This looks simple, but it teaches a habit that shows up everywhere in programming.
When several rules could match, the first true rule wins. That means the order of the checks is part of the solution, not just a formatting choice.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.