Skip to content
Visual explainer 10h to learn 5 steps

Timed problem-solving sessions

Solve mixed-topic medium problems within 30 to 45 minutes each using a repeatable method.

By Invicly Tech, , in Data Structures & Algorithms. About 12 minutes to read.

Topic 53 of 54

In short

Timed sessions train you to solve unfamiliar problems under pressure with a repeatable method. Clarify the problem, work examples, pick a pattern, state complexity, code, then test with edge cases. Logging every miss shows which patterns need more practice.

Explained simply

Knowing patterns is one skill. Using them on a new problem, with a clock running and someone watching, is another. Timed sessions train that second skill. You pick problems you have not seen, set a timer for 30 to 45 minutes each, and work through them with the same steps every time.

The steps are simple: clarify the problem, try small examples by hand, plan an approach and its cost, write the code, then test it. Having a fixed routine means you never freeze. When you are stuck, you just move to the next step of the routine.

Talk out loud while you work, even when alone. In an interview, the person listening wants to see how you think, not only the final code. Practising this makes it natural, and saying your idea aloud often reveals a flaw before you write a line.

After each session, write down what went wrong in a mistake log. A week later, solve the failed problems again from a blank file. This is where most of the learning happens.

Press Play on a diagram to walk through it one step at a time.

FlowA 40-minute problem routine
  1. 1
    Clarify (5 min)

    Inputs, constraints, edge cases.

  2. 2
    Examples (5 min)

    Work small cases by hand.

  3. 3
    Plan (5 min)

    Brute force, then a better pattern and its Big-O.

  4. 4
    Code (15 min)

    Clean, named variables.

  5. 5
    Test (10 min)

    Walk through edge cases and fix.

CompareThree common reasons a timed problem fails
Did not see the pattern
  • Signal was missed
  • Fix: drill pattern recognition
  • Add to the cheat sheet
Right idea, buggy code
  • Off-by-one or edge case
  • Fix: trace a table before running
  • Write the bug in the log
Too slow to finish
  • Spent too long planning or coding
  • Fix: stricter time boxes
  • Practise standard templates

In depth

Let the constraints choose the complexity

The input limits in a problem are a strong hint about the expected solution. A useful rule: a fast compiled language does roughly 10^8 simple operations per second, and Python closer to 10^7. Most judges allow one or two seconds.

So if n can be 10^5, an O(n^2) solution needs about 10^10 steps and will time out; you need O(n log n) or O(n). If n is at most 20, 2^20 is about a million, so trying every subset is fine. If n is at most 500, O(n^3) is about 1.25 * 10^8, which is borderline in Python. If n can be 10^9 or more, you cannot even read every value, so think O(log n) (binary search) or a formula.

Read the limits before planning. They tell you whether to look for a smarter pattern at all, and which family to look in.

MapTypical target complexity by input size
Largest n
  • Up to about 12
    • O(n!)
    • All permutations
  • Up to about 20
    • O(2^n)
    • All subsets, bitmask DP
  • Up to about 500
    • O(n^3)
    • Interval or Floyd style DP
  • Up to about 5,000
    • O(n^2)
    • Pairs, 2D DP
  • Up to about 10^6
    • O(n log n) or O(n)
    • Sort, heap, two pointers, hashing
  • 10^9 and above
    • O(log n) or O(1)
    • Binary search, math

Clarify: the questions worth asking

Spend the first few minutes making the problem precise. Ask about size: how large can n get, and how large are the values? Ask about shape: can the input be empty, can it contain negatives or duplicates, is it already sorted? Ask about the output: return indexes or values, any order or a specific one, what to return when there is no answer, and how to break ties. Ask about rules: may you change the input in place, and is extra memory allowed?

Then restate the problem in your own words and work one small example by hand, ideally one you make up, not the one given. Many wrong solutions come from a misunderstanding that a single hand-worked example would have exposed.

Alone, write the answers as comments at the top of your file. It builds the habit for real interviews, where the interviewer often hides a detail on purpose to see if you ask.

  • Size of n and of values
  • Empty, negative, duplicate, sorted?
  • Exact output format and ties
  • In place allowed? Memory limits?

Plan and think out loud

Always say the brute force first, with its complexity: "Checking every pair is O(n^2), which is too slow for n = 10^5." It proves you have a correct baseline and frames the goal. Next, find the bottleneck: what work is repeated? Repeated lookups suggest a hash map. Repeated range sums suggest prefix sums. Repeated "best so far" choices suggest a heap or greedy. Overlapping subproblems suggest dynamic programming.

State the final plan and its time and space before coding, and get agreement if someone is listening. While coding, narrate briefly: what this loop maintains, why this index starts at 1. When stuck, say what you tried and why it fails; that often unlocks the next idea, and interviewers can only give hints when they know where you are.

Use clear names like left, right and best, not i2 and tmp. Clean code is easier to test and to explain.

Test like a reviewer

Do not run the code in your head the way you meant it to work. Trace it line by line on a small input and write down the variables after each loop step, like a table. This catches most off-by-one errors.

Then try edge cases: empty input, one element, two elements, all equal values, the answer at the very start or end, and the largest allowed input for complexity. Python has a few traps of its own: recursion stops at a depth of about 1000 unless you raise sys.setrecursionlimit, [[0] * m] * n creates n references to the same row, and a mutable default argument such as def f(memo={}) is shared across calls.

If you find a bug, fix it calmly and re-run the same trace. In an interview, finding your own bug is a positive signal, not a failure.

  • Trace variables in a table
  • Empty, single, duplicates, extremes
  • [[0] * m for _ in range(n)], not [[0] * m] * n
  • Recursion depth limit is about 1000 by default

Sessions, mistake log and the one-week revisit

A good session mixes topics so you practise recognising the pattern, not just applying the one from today's chapter. For example: three medium problems in 2 hours, drawn from different topics, with a hard stop at 45 minutes each.

When time runs out, stop. Read a good solution, understand why it works, then close it and write it yourself. Log the problem: date, pattern, what went wrong, the one insight you were missing, and a date to retry. Classify the failure: did not recognise the pattern, chose a wrong approach, had the idea but a bug, or was too slow. Each type needs a different fix.

A week later, solve it again from a blank file, under the timer. If you solve it, schedule another review further out. If not, reset and try again in a few days.

CycleThe practice loop
  1. 1
    Attempt

    Timed, from a blank file.

  2. 2
    Stop at the limit

    Solved or not.

  3. 3
    Study

    Understand the missing insight.

  4. 4
    Log

    Pattern, failure type, retry date.

  5. 5
    Re-solve in a week

    Without looking at notes.

  6. Back to step 1

Learn it in 5 short steps

About 2h each. Track them with a timer in your plan.

  1. 1
    Use a clarify, plan, code, test method~2h
  2. 2
    Solve mixed sets under a timer~2h
  3. 3
    Explain the approach out loud~2h
  4. 4
    Log mistakes and pattern misses~2h
  5. 5
    Revisit failed problems after a week~2h

Key points

  • Input constraints hint at the target complexity: n = 10^5 needs about O(n log n).
  • Say the brute force and its complexity before optimising.
  • Stop after the time limit, study the solution, and retry in a week.
  • Mix topics so you practise recognising patterns, not just applying them.

Key terms

Tap a card to flip it. 0/3 known

Examples

One 40-minute run: top k frequent elements

Clarify (4 min): n up to 10^5, k is valid, any order is fine, ties can go either way. Example: [1, 1, 1, 2, 2, 3] with k = 2 gives [1, 2]. Plan (6 min): counting is O(n). Sorting the counts is O(n log n); a heap of size k is O(n log k); but counts are between 1 and n, so bucket them by count for O(n). Code (15 min): below. Test (10 min): trace the example; then k equal to the number of distinct values, all elements equal, and one element.

python
from collections import Counter

def top_k_frequent(nums, k):      # O(n) time and space
    count = Counter(nums)
    buckets = [[] for _ in range(len(nums) + 1)]
    for x, c in count.items():
        buckets[c].append(x)
    out = []
    for c in range(len(buckets) - 1, 0, -1):
        for x in buckets[c]:
            out.append(x)
            if len(out) == k: return out
    return out

print(top_k_frequent([1, 1, 1, 2, 2, 3], 2))   # [1, 2]

Turning a failed attempt into a useful log entry

You spent 45 minutes on "subarray sum equals k" and your sliding window gave wrong answers. A weak log entry says "wrong answer". A useful one names the cause and the insight: the window broke because numbers can be negative. Step 1: write the entry. Step 2: study the prefix sum plus hash map approach and re-code it from memory. Step 3: set the retry date a week out. Step 4: add "negatives present, so no sliding window" to your pattern cheat sheet.

text
date:      2026-11-03
problem:   Subarray Sum Equals K
pattern:   prefix sums + hash map
failure:   wrong approach (tried sliding window)
insight:   negatives break window monotonicity;
           count[prefix - k] gives the answer in O(n)
retry on:  2026-11-10
result:    solved in 18 min

Quick reference

  • Routine: clarify, examples, plan with Big-O, code, test; never skip clarify
  • n up to 10^5 or 10^6 needs O(n log n) or better; O(n^2) is about 10^10 steps
  • n up to 20 allows O(2^n); n up to 12 allows O(n!)
  • Say the brute force and its cost first, then name the bottleneck
  • Trace a small input in a variable table before running anything
  • Python traps: recursion limit near 1000, [[0] * m] * n shares rows, mutable defaults
  • Hard stop at the time limit; study, log the insight, re-solve in a week

Common mistakes

  • Coding immediately without clarifying constraints.
  • Reading solutions too early, so no struggle builds intuition.
  • Never revisiting failed problems.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A problem says n is at most 20. Which approach is usually acceptable?

Question 2

2What should you do in the first minutes of a timed problem?

Question 3

3n can be 10^5 and your idea is O(n^2). What is the right move?

Question 4

4Which mistake-log entry is most useful?

Question 5

5In Python, what does grid = [[0] * 3] * 3 followed by grid[0][0] = 1 produce?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    For each limit, write the target complexity: (a) n at most 15, (b) n at most 2,000, (c) n at most 2 * 10^5, (d) n at most 10^18.

  2. Exercise 2medium

    Before coding "longest consecutive sequence" (unsorted list, return the length of the longest run of consecutive integers in O(n)), spend 10 minutes writing the clarify questions, the brute force with its cost, and the better plan.

  3. Exercise 3hard

    Solve "merge intervals" in 30 minutes using the full routine, and finish with asserts for the edge cases you found.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

How many problems should I solve per day?
Quality beats count. Two or three medium problems done with the full routine, a mistake log and a later re-solve teach more than ten problems where you read the answer early.
When should I look at the solution?
After the time limit, or after about 15 to 20 minutes with no new idea at all. Then study it, close it and write it yourself.
Is it okay to solve problems in Python for interviews?
Yes. Most companies allow it, and its short syntax saves time. Know its built-ins well (collections, heapq, bisect) and the costs of common operations like list.pop(0), which is O(n).
How do I practise explaining out loud when I study alone?
Talk to yourself or record your screen with audio, then watch one recording a week. Long silences and unexplained jumps in the recording show exactly what to work on.

Go deeper

ShareXLinkedInWhatsApp