Skip to content

Commit 4dee717

Browse files
committed
Add advanced and expert assessments
1 parent b04ee83 commit 4dee717

8 files changed

Lines changed: 262 additions & 4 deletions

File tree

languages/cpp/03-advanced/PROGRESS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Use this file as your personal progress log for this level.
1313
## Level Milestones
1414

1515
- [ ] Completed capstone: `languages/cpp/projects/03-advanced`
16-
- [ ] (Optional for now) add and complete `languages/cpp/assessments/03-advanced`
16+
- [ ] Completed assessment: `languages/cpp/assessments/03-advanced`
1717
- [ ] Updated global checklist: `languages/cpp/CHECKLIST.md`
1818

1919
## My Struggles And Fixes

languages/cpp/04-expert/PROGRESS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Use this file as your personal progress log for this level.
1313
## Level Milestones
1414

1515
- [ ] Completed capstone: `languages/cpp/projects/04-expert`
16-
- [ ] (Optional for now) add and complete `languages/cpp/assessments/04-expert`
16+
- [ ] Completed assessment: `languages/cpp/assessments/04-expert`
1717
- [ ] Updated global checklist: `languages/cpp/CHECKLIST.md`
1818

1919
## My Struggles And Fixes

languages/cpp/CHECKLIST.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
- [ ] Complete `03-advanced/inheritance-and-polymorphism`.
4141
- [ ] Complete `03-advanced/templates-basics`.
4242
- [ ] Complete capstone `projects/03-advanced`.
43-
- [ ] Complete assessment `assessments/03-advanced` (when available).
43+
- [ ] Complete assessment `assessments/03-advanced`.
4444
- [ ] Update `03-advanced/PROGRESS.md` with session notes.
4545

4646
## 04-expert
@@ -51,5 +51,5 @@
5151
- [ ] Complete `04-expert/performance-and-profiling-basics`.
5252
- [ ] Complete `04-expert/modularization-and-build-structure`.
5353
- [ ] Complete capstone `projects/04-expert`.
54-
- [ ] Complete assessment `assessments/04-expert` (when available).
54+
- [ ] Complete assessment `assessments/04-expert`.
5555
- [ ] Update `04-expert/PROGRESS.md` with session notes.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Assessment 03: Advanced
2+
3+
## Goal
4+
5+
Combine class design, polymorphism, and templates in one practical program.
6+
7+
## Task
8+
9+
Write a program that:
10+
11+
1. Defines a polymorphic `Shape` hierarchy (`Circle`, `Rectangle`).
12+
2. Stores shapes in `vector<unique_ptr<Shape>>`.
13+
3. Calculates each shape area and total area.
14+
4. Uses a template helper to print any numeric vector.
15+
5. Prints min and max area values.
16+
17+
## Quick Run
18+
19+
```bash
20+
g++ -std=c++17 -Wall -Wextra -pedantic main.cpp -o assessment_03_advanced
21+
./assessment_03_advanced
22+
```
23+
24+
## Expected Output (shape)
25+
26+
```text
27+
Shape areas:
28+
- Circle: ...
29+
- Rectangle: ...
30+
Total area: ...
31+
Minimum area: ...
32+
Maximum area: ...
33+
```
34+
35+
## What To Check
36+
37+
- Polymorphic calls happen through base pointers.
38+
- No raw owning pointers are used.
39+
- Template helper works for both `vector<int>` and `vector<double>`.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <algorithm>
2+
#include <numeric>
3+
#include <cmath>
4+
#include <iostream>
5+
#include <memory>
6+
#include <string>
7+
#include <vector>
8+
9+
using namespace std;
10+
11+
class Shape {
12+
public:
13+
virtual ~Shape() = default;
14+
virtual double area() const = 0;
15+
virtual string name() const = 0;
16+
};
17+
18+
class Circle : public Shape {
19+
public:
20+
explicit Circle(double radiusValue) : radius(radiusValue) {}
21+
22+
double area() const override {
23+
return 3.141592653589793 * radius * radius;
24+
}
25+
26+
string name() const override {
27+
return "Circle";
28+
}
29+
30+
private:
31+
double radius;
32+
};
33+
34+
class Rectangle : public Shape {
35+
public:
36+
Rectangle(double widthValue, double heightValue)
37+
: width(widthValue), height(heightValue) {}
38+
39+
double area() const override {
40+
return width * height;
41+
}
42+
43+
string name() const override {
44+
return "Rectangle";
45+
}
46+
47+
private:
48+
double width;
49+
double height;
50+
};
51+
52+
template <typename T>
53+
void printVector(const vector<T>& values, const string& label) {
54+
cout << label << ": [";
55+
for (size_t i = 0; i < values.size(); ++i) {
56+
cout << values[i];
57+
if (i + 1 < values.size()) {
58+
cout << ", ";
59+
}
60+
}
61+
cout << "]\n";
62+
}
63+
64+
int main() {
65+
vector<unique_ptr<Shape>> shapes;
66+
shapes.push_back(make_unique<Circle>(2.0));
67+
shapes.push_back(make_unique<Rectangle>(3.0, 4.0));
68+
shapes.push_back(make_unique<Circle>(1.5));
69+
70+
vector<double> areas;
71+
areas.reserve(shapes.size());
72+
73+
cout << "Shape areas:\n";
74+
for (const unique_ptr<Shape>& shape : shapes) {
75+
const double value = shape->area();
76+
areas.push_back(value);
77+
cout << "- " << shape->name() << ": " << value << '\n';
78+
}
79+
80+
const double totalArea = accumulate(areas.begin(), areas.end(), 0.0);
81+
const double minArea = *min_element(areas.begin(), areas.end());
82+
const double maxArea = *max_element(areas.begin(), areas.end());
83+
84+
cout << "Total area: " << totalArea << '\n';
85+
cout << "Minimum area: " << minArea << '\n';
86+
cout << "Maximum area: " << maxArea << '\n';
87+
88+
vector<int> sampleCounts = {1, 2, 3, 4};
89+
printVector(sampleCounts, "Sample counts");
90+
printVector(areas, "Computed areas");
91+
92+
return 0;
93+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Assessment 04: Expert
2+
3+
## Goal
4+
5+
Practice ownership, thread-safe work distribution, and result aggregation.
6+
7+
## Task
8+
9+
Write a program that:
10+
11+
1. Uses `unique_ptr` to manage a shared data container.
12+
2. Splits work across multiple threads.
13+
3. Uses `mutex` to protect shared aggregation state.
14+
4. Computes:
15+
- total sum
16+
- minimum value
17+
- maximum value
18+
5. Prints per-thread partial sums and final summary.
19+
20+
## Quick Run
21+
22+
```bash
23+
g++ -std=c++17 -Wall -Wextra -pedantic -pthread main.cpp -o assessment_04_expert
24+
./assessment_04_expert
25+
```
26+
27+
On Linux, `-pthread` is required for thread support.
28+
29+
## Expected Output (shape)
30+
31+
```text
32+
Worker 0 partial sum: ...
33+
Worker 1 partial sum: ...
34+
...
35+
Final summary:
36+
Total: ...
37+
Minimum: ...
38+
Maximum: ...
39+
```
40+
41+
## What To Check
42+
43+
- No data races (shared updates protected by `mutex`).
44+
- Ownership is explicit (`unique_ptr`).
45+
- Threads are always joined before program exit.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <algorithm>
2+
#include <iostream>
3+
#include <limits>
4+
#include <memory>
5+
#include <mutex>
6+
#include <thread>
7+
#include <vector>
8+
9+
using namespace std;
10+
11+
struct Summary {
12+
long long total = 0;
13+
int minimum = numeric_limits<int>::max();
14+
int maximum = numeric_limits<int>::min();
15+
};
16+
17+
int main() {
18+
auto data = make_unique<vector<int>>(vector<int>{
19+
12, 7, 25, 4, 31, 19, 2, 45, 18, 9, 27, 6
20+
});
21+
22+
const size_t threadCount = 3;
23+
vector<thread> workers;
24+
workers.reserve(threadCount);
25+
26+
Summary globalSummary;
27+
mutex summaryMutex;
28+
29+
const size_t chunkSize = (data->size() + threadCount - 1) / threadCount;
30+
31+
for (size_t workerId = 0; workerId < threadCount; ++workerId) {
32+
const size_t begin = workerId * chunkSize;
33+
const size_t end = min(begin + chunkSize, data->size());
34+
35+
workers.emplace_back([workerId, begin, end, &data, &globalSummary, &summaryMutex]() {
36+
if (begin >= end) {
37+
return;
38+
}
39+
40+
long long localTotal = 0;
41+
int localMin = (*data)[begin];
42+
int localMax = (*data)[begin];
43+
44+
for (size_t i = begin; i < end; ++i) {
45+
const int value = (*data)[i];
46+
localTotal += value;
47+
if (value < localMin) {
48+
localMin = value;
49+
}
50+
if (value > localMax) {
51+
localMax = value;
52+
}
53+
}
54+
55+
{
56+
lock_guard<mutex> lock(summaryMutex);
57+
cout << "Worker " << workerId << " partial sum: " << localTotal << '\n';
58+
globalSummary.total += localTotal;
59+
if (localMin < globalSummary.minimum) {
60+
globalSummary.minimum = localMin;
61+
}
62+
if (localMax > globalSummary.maximum) {
63+
globalSummary.maximum = localMax;
64+
}
65+
}
66+
});
67+
}
68+
69+
for (thread& worker : workers) {
70+
worker.join();
71+
}
72+
73+
cout << "\nFinal summary:\n";
74+
cout << "Total: " << globalSummary.total << '\n';
75+
cout << "Minimum: " << globalSummary.minimum << '\n';
76+
cout << "Maximum: " << globalSummary.maximum << '\n';
77+
78+
return 0;
79+
}

languages/cpp/assessments/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Use these assessments after finishing each level to check practical readiness.
66

77
1. [01-foundations](./01-foundations/README.md)
88
2. [02-core](./02-core/README.md)
9+
3. [03-advanced](./03-advanced/README.md)
10+
4. [04-expert](./04-expert/README.md)
911

1012
Each assessment contains:
1113

0 commit comments

Comments
 (0)