Skip to content

DoctorLai/cpp-coding-exercise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cpp-coding-exercise

C++ Build Last Commit License Stars Code Style Commit Activity Watchers PRs Welcome Repo Size Top Language Open PRs Forks Open Issues Ask DeepWiki

A collection of modern C++ coding examples and small exercises focused on correctness, clarity, and real-world patterns.

This repository is intended as:

  • An interview-preparation playground
  • A reference for concurrency, synchronization, low-level, and modern C++ techniques
  • A growing set of self-contained, buildable examples
  • A collection of practical examples and CLI tools rather than puzzle-only solutions

Current examples include:


Repository Structure

cpp-coding-exercise/
├── Makefile              # top-level dispatcher
├── common.mk             # shared compiler flags
├── <example>/
│   ├── Makefile
│   ├── main.cpp
│   └── tests.sh           # optional behavior tests
└── .github/workflows/
    ├── ci.yml             # builds, sanitizers, tests, and coverage
    └── coverage-comment.yml

Design principles

  • Each example is self-contained
  • Each folder has its own Makefile
  • The top-level Makefile automatically discovers and builds all examples
  • No central list to maintain when adding new folders

Prerequisites

sudo apt update
sudo apt install g++-14 make clang-format gcovr jq libtbb-dev

The examples target C++23 and are tested on Ubuntu with GCC 14.

Building

Build everything

make

This will recursively build every directory that contains a Makefile.

Build a single example

cd thread-safe-queue
make

Running examples

Runnable examples expose a run target:

make run

Top-level make run runs all built targets and each available tests.sh script.

Project commands

make build             # Build every example with the selected sanitizer
make test              # Build as needed, run examples, and execute test scripts
make format            # Format all C and C++ source files
make lint              # Check formatting without modifying files
make coverage          # Generate HTML, XML, JSON, and text coverage reports
make check             # Run formatting and supported sanitizer builds/tests
make clean             # Remove binaries, objects, and coverage artifacts

make coverage enforces at least 80% line coverage, 80% function coverage, and 70% branch coverage by default. Override local thresholds with COVERAGE_MIN, COVERAGE_FUNCTION_MIN, and COVERAGE_BRANCH_MIN.


Cleaning

Clean everything:

make clean

Or clean a single example:

cd thread-safe-queue
make clean

Shared build configuration

Common compiler settings live in common.mk:

CXX      := g++-14
CXXFLAGS := -std=c++23 -Wall -Wextra -Werror

Individual examples may extend this, e.g.:

CXXFLAGS += -pthread

Sanitizers

AddressSanitizer is enabled by default to detect memory safety problems and leaks.

# Builds with AddressSanitizer automatically
make

# ThreadSanitizer
make SANITIZE=thread

# UndefinedBehaviorSanitizer
make SANITIZE=undefined

# Select the sanitizer matrix used by make check
make check SANITIZERS="address thread undefined"

# No sanitizers
make SANITIZE=

GCC ThreadSanitizer cannot reliably reserve its shadow-memory layout under WSL2 and may terminate before an example starts with FATAL: ThreadSanitizer: unexpected memory mapping. On WSL2, make check therefore runs ASan and UBSan by default. Native Linux runs ASan, TSan, and UBSan. SANITIZERS can override either default.

The parallel-transform example uses libstdc++'s parallel STL backend, which delegates to oneTBB. TSan requires oneTBB itself to be sanitizer-aware so that its internal synchronization is visible. The dedicated CI TSan job therefore builds the pinned oneTBB release with TBB_SANITIZE=thread instead of using Ubuntu's ordinary runtime library.


Clang Format

clang-format enforces the repository's C++ style.

make format            # Apply formatting
make lint              # Check all example directories
./clang-check.sh       # Equivalent recursive standalone check

Each example also exposes make check-format from its own directory.


Continuous Integration

GitHub Actions builds all examples on:

  • Pushes to main
  • Pull requests targeting main

The CI setup requires no updates when new example folders are added.

CI runs ASan and UBSan in one job and TSan with a sanitizer-aware oneTBB build in another. It also enforces the documented coverage floors, uploads an HTML coverage report, and posts a coverage summary on pull requests. Each sanitizer configuration starts from a clean build so compiler flags cannot be silently reused from a previous configuration.


Toolchain

  • C++23
  • GNU Make
  • GCC 14
  • clang-format
  • gcovr
  • Linux (Ubuntu)

Goals (non-goals)

  • ✔ Correctness over cleverness

  • ✔ Explicit concurrency semantics

  • ✔ Minimal dependencies

  • ✘ No frameworks

  • ✘ No large abstractions

  • ✘ No header‑only meta‑programming for its own sake


Contributing and support

See CONTRIBUTING.md before submitting a change. For help, use the channels in SUPPORT.md. Report security concerns privately as described in SECURITY.md.

Project changes are recorded in CHANGELOG.md, and the repository's data practices are documented in PRIVACY.md.

License

MIT (unless otherwise stated in a specific example).


Notes

Many examples intentionally focus on edge cases and failure modes such as data races, lifetime issues, and ordering bugs. They are meant to be read, built, and experimented with.

Contributions and discussions are welcome.

About

C++ Coding Exercise

Topics

Resources

License

Contributing

Security policy

Stars

3 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors