Skip to content

Come Back Later

The Course
•
30 min study

Return about a week after you finish and again about a month later. These challenges are extra review, outside Day 7's time estimate. Reconstructing code from a brief gives far better evidence of what you remember than rereading a familiar solution, which only feels like knowing it. If you get stuck, that is useful information: it tells you exactly which lesson to revisit.

Work through the three challenges in order, without opening earlier lessons. Write your answer, run it, and only then open the hint to compare.

Retrieve: define a Task record with a title, minutes and a done flag, all with sensible defaults. Write a helper that takes the tasks by const reference and totals the minutes of incomplete tasks. Assert a mix, an empty vector and an all-done vector. Start from the blank editor:

Retrieve: your working space C++
#include <iostream>
#include <string>
#include <vector>
#include <cassert>

int main() {
    // Define Task and your helper above main, then assert here.
    return 0;
}
Check your answer

Compare with your code: each field needs a default (int minutes = 0;, bool done = false;) so Task{} is fully initialized; the parameter is const std::vector<Task>&, so the helper can't change the plan or copy it; the loop variable is const Task&; and the empty and all-done vectors both give 0. If you forgot any of these, Day 5 is the lesson to revisit.

Debug: this loop is meant to mark every task done, but nothing changes. Run it, then write down why before you change anything:

Debug: why does nothing change? C++
#include <iostream>
#include <string>
#include <vector>

struct Task {
    std::string title;
    int minutes = 0;
    bool done = false;
};

int main() {
    std::vector<Task> tasks{{"Read", 25, false}, {"Review", 10, false}};
    for (Task task : tasks) {
        task.done = true;
    }
    for (const Task& task : tasks) {
        std::cout << task.title << " " << task.done << "\n";
    }
    return 0;
}
Check your explanation and fix

The loop variable is a copy of each element, so the assignment changes the copy, which is destroyed at the end of each iteration. Adding & makes task a reference to the element itself:

C++
#include <iostream>
#include <string>
#include <vector>

struct Task {
    std::string title;
    int minutes = 0;
    bool done = false;
};

int main() {
    std::vector<Task> tasks{{"Read", 25, false}, {"Review", 10, false}};
    for (Task& task : tasks) {
        task.done = true;
    }
    for (const Task& task : tasks) {
        std::cout << task.title << " " << task.done << "\n";
    }
    return 0;
}
Text
Read 1
Review 1

Then, without running such code, explain why this function is invalid and how to fix it:

C++
const std::vector<int>& make_plan() {
    std::vector<int> plan{25, 10};
    return plan;
}
Check your explanation

plan is a local, so its lifetime ends when the function returns. The returned reference dangles, and using it is undefined behavior. Returning std::vector<int> by value fixes it: the caller receives its own vector.

Transfer: write count_in_range for a vector of integers with inclusive low and high bounds. Make one case that fails if && is replaced with ||, and one that fails if an endpoint is excluded.

Practice

Exercise

Your turn

Implement int count_in_range(const std::vector<int>& values, int low, int high). Assert:

  • 0, 1, 10 and 11 in the range 1 through 10 give 2
  • an empty vector gives 0
  • a reversed range, low 10 and high 1, gives 0 for the values 1 and 10
  • a single-value range, 5 through 5, counts only the 5s in 4, 5, 5 and 6, giving 2

When all pass, print Review checks passed.

Your workspace C++
#include <iostream>
#include <vector>
#include <cassert>

int main() {
    // Write count_in_range above main and your assertions here.
    return 0;
}
Hint 1: the idea

An accepted value must satisfy both bounds. A reversed range needs no special code: no value can be at least 10 and at most 1.

Hint 2: a first step

Write the assertions first, before the function body: one ordinary case, the reversed range, and a range of one value. Give the function a body that just returns 0 and run: the cases that expect 0 pass, which shows they cannot catch everything on their own.

Show one solution
Run the solution C++
#include <iostream>
#include <vector>
#include <cassert>

int count_in_range(const std::vector<int>& values, int low, int high) {
    int count = 0;
    for (int value : values) {
        if (value >= low && value <= high) {
            ++count;
        }
    }
    return count;
}

int main() {
    assert(count_in_range({0, 1, 10, 11}, 1, 10) == 2);
    assert(count_in_range({}, 1, 10) == 0);
    assert(count_in_range({1, 10}, 10, 1) == 0);
    assert(count_in_range({4, 5, 5, 6}, 5, 5) == 2);
    std::cout << "Review checks passed\n";
    return 0;
}
Text
Review checks passed

The reversed range returns 0 naturally, because && requires both comparisons, and the single-value range shows that inclusive bounds really do include their endpoints. With ||, the first, third and fourth assertions would all fail, though assert stops at the first; with > instead of >=, the first and last would, and again only the first is reported. If you wrote these cases before looking, your testing habits from Day 3 have stuck.

Predict and run

Predict before running C++
#include <iostream>
#include <vector>

int main() {
    std::vector<int> values{0, 1, 10, 11};
    int count = 0;
    for (int value : values) {
        if (value >= 1 && value <= 10) {
            ++count;
        }
    }
    std::cout << count << "\n";
    return 0;
}
Check your prediction
Text
2

Predict the result with || before running that change. Every integer is either at least 1 or at most 10, so all four values count and the result is 4. The input deliberately includes one value just outside each bound and one on each bound, so it catches both the wrong operator and an off-by-one at either end. Restore && and say the rule in words: a value counts when it satisfies both bounds.

Decide what to revisit

  • If the copy loop or the dangling reference was unclear, return to Day 5's lessons on copies and lifetimes before learning more advanced ownership tools.
  • If the range cases were hard to choose, revisit Day 2's range validation and Day 3's assertions.
  • This page did not review parsing. If Day 6's text parsing felt shaky, state the accepted format and the failure behavior in writing before adding features, then reread Day 6.

Useful next topics include classes that protect their own rules, standard algorithms, richer error handling and smart pointers. C remains a separate course; it is not a prerequisite for this one.

Was this article helpful?

Your response is saved on this device.