# Cracking The Coding Interview Solutions With Automated Unit Tests

**URL:** <https://onehack.st/t/cracking-the-coding-interview-solutions-with-automated-unit-tests/287816>\
**Category:** Tutorials & Methods\
**Tags:** tools, programming\
**Created:** [July 28, 2024, 8:12pm UTC](https://onehack.st/t/cracking-the-coding-interview-solutions-with-automated-unit-tests/287816 "2024-07-28T20:12:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheStrength](https://onehack.st/user_avatar/onehack.st/thestrength/32/46753_2.png) [@TheStrength](https://onehack.st/u/TheStrength)\
**Post date:** [July 28, 2024, 8:12pm UTC](https://onehack.st/t/cracking-the-coding-interview-solutions-with-automated-unit-tests/287816/1 "2024-07-28T20:12:50Z")

</div>

## Cracking The Coding Interview Solutions with Automated Unit Tests

- ✅ C++ Solutions
- ✅ Python Solutions
- ✅ Automated Unit Tests
- ✅ Active Development
- ✅ Multiplatform Support: Mac, Linux, and Windows

### Introduction

This repo contains C++ and Python solutions for Gayle Laakmann McDowell’s [_Cracking the Coding Interview 6th Edition_](http://a.co/baneyGe). Admittedly, there are other GitHub repositories with solutions for this book. _But how do you know that their code is actually correct?_ If it’s untested, then you don’t!

In this project, every C++ solution has unit tests using the [C++ Catch framework](https://github.com/philsquared/Catch), and every Python solution has unit tests using the [Python unittest framework](https://docs.python.org/3.6/library/unittest.html). We enforce test correctness automatically using [continuous integration servers](https://travis-ci.org/alexhagiopol/cracking-the-coding-interview) ensuring that the solutions are made of _living_ code that gets executed and tested on every single commit. To my knowledge, this is the Internet’s only solutions repository with this level of testing rigor: \>90% automated test coverage means you can reference and contribute solutions with confidence.

### Table of Contents

#### [C++ Solutions](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/cpp_solutions):

1. [C++ Unit Tests](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/tests.cpp)
2. [Chapter 1 - Arrays and Strings](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/cpp_solutions/chapter_01_arrays_and_strings): `9 / 9` complete.
3. [Chapter 2 - Linked Lists](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/cpp_solutions/chapter_02_linked_lists): `8 / 8` complete.
4. [Chapter 3 - Stacks and Queues](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/cpp_solutions/chapter_03_stacks_and_queues): `4 / 6` complete.
5. [Chapter 4 - Trees and Graphs](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/cpp_solutions/chapter_04_trees_and_graphs): `11 / 12` complete.
6. [Chapter 5 - Bit Manipulation](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/cpp_solutions/chapter_05_bit_manipulation): `7 / 8` complete.
7. Chapter 6 - Math and Logic: `0 / 10` complete.
8. Chapter 7 - Object Oriented Design: `0 / 12` complete.
9. [Chapter 8 - Recursion and Dynamic Programming](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/cpp_solutions/chapter_08_recursion_and_dynamic_programming): `8 / 14` complete.
10. Chapter 9 - System Design and Scalability: `0 / 8` complete.
11. [Chapter 10 - Sorting and Searching](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/cpp_solutions/chapter_10_sorting_and_searching): `10 / 11` complete.
12. Chapter 11 - Testing: `0 / 6` complete.
13. [Chapter 12 - C and C++](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/cpp_solutions/chapter_12_cpp): `10 / 11`
14. Chapter 13 - Java: N/A
15. Chapter 14 - Databases: `0 / 7` complete.
16. Chapter 15 - Threads and Locks: `0 / 4` complete.
17. [Chapter 16 - Moderate](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/cpp_solutions/chapter_16_moderate): `11 / 26` complete.
18. [Chapter 17 - Hard](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/cpp_solutions/chapter_17_hard): `1 / 26` complete.
19. [Miscellaneous Exercises](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/cpp_solutions/misc_exercises): `2` complete.

C++ Total: `81` solutions complete.

#### [Python Solutions](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/python_solutions):

1. [Python Unit tests](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/tests.py)
2. [Chapter 1 - Arrays and Strings](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/python_solutions/chapter_01_arrays_and_strings): `9 / 9` complete.
3. [Chapter 2 - Linked Lists](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/python_solutions/chapter_02_linked_lists): `8 / 8` complete.
4. [Chapter 3 - Stacks and Queues](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/python_solutions/chapter_03_stacks_queues): `6 / 6` complete.
5. [Chapter 4 - Trees and Graphs](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/python_solutions/chapter_04_trees_and_graphs): `11 / 12` complete.
6. [Chapter 5 - Bit Manipulation](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/python_solutions/chapter_05_bit_manipulation): `7 / 8` complete.
7. Chapter 6 - Math and Logic: `0 / 10` complete.
8. Chapter 7 - Object Oriented Design: `0 / 12` complete.
9. [Chapter 8 - Recursion and Dynamic Programming](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/python_solutions/chapter_08_recursion_and_dynamic_programming): `11 / 14` complete.
10. Chapter 9 - System Design and Scalability: `0 / 8` complete.
11. [Chapter 10 - Sorting and Searching](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/python_solutions/chapter_10_sorting_and_searching): `1 / 11` complete.
12. Chapter 11 - Testing: `0 / 6` complete.
13. Chapter 12 - C and C++: N/A
14. Chapter 13 - Java: N/A
15. Chapter 14 - Databases: `0 / 7` complete.
16. Chapter 15 - Threads and Locks: N/A
17. [Chapter 16 - Moderate](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/python_solutions/chapter_16_moderate): `2 / 26` complete.
18. [Chapter 17 - Hard](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/python_solutions/chapter_17_hard): `3 / 26` complete.

Python Total: `58` solutions complete.

##### Grand Total: `139` unique solutions complete.

### Building:

#### Mac:

Mac usage requires installing the package managers Homebrew and Pip which is done for you in the Makefile:

```auto
git clone https://github.com/alexhagiopol/cracking-the-coding-interview.git ctci
cd ctci
make configure-mac

```

#### Ubuntu:

```auto
git clone https://github.com/alexhagiopol/cracking-the-coding-interview.git
cd ctci
make configure-ubuntu

```

#### Windows:

For Windows users, I recommend developing this project using the [Windows Subsystem for Linux (WSL)](https://docs.microsoft.com/en-us/windows/wsl/install-win10) feature of Windows 10 and then following the Ubuntu build and testing instructions inside WSL. This recommendation is due to the use of Ubuntu, CMake, and makefile based build and execution systems on the servers that automatically test changes to the code. For more on Windows development, see the `Appendix` section at the end of this page.

### Testing:

Testing is the most important part of this project: _only_ the unit tests define the problem being solved. In the root directory, execute the following to run all tests in Python and C++:

```auto
make test

```

`make test` is the command I use most while developing this project. My workflow follows:

1. I read a problem and encode the problem definition by implementing its unit tests.
2. I execute `make test` to see where the project fails to satisfy the unit tests.
3. I add to the implementation of the problem solution until `make test` succeeds.

### Optional: Generating a Test Coverage % Report Locally (Python support only):

This will show exactly which lines are not covered by tests in each Python source file:

```auto
pip install coverage
make test_coverage

```

### Contributing

The goal of this project is to write a tested Python and C++ solution for every problem in the 6th edition of the textbook. I definitely need help, so PRs are welcome. There is much to do because each solution needs its own small dataset and infrastructure in order to be tested. Here are some ways you can help:

1. Fixing bugs 🐛.
2. Solving or improving solutions to problems in either language (see the completion progress in the table of contents).
3. Adding more unit tests to increase the test coverage %.
4. Implementing automated C++ test coverage measurement using [gcov](http://gcc.gnu.org/onlinedocs/gcc/Gcov.html).
5. Integrating automated test coverage % reporting for both Python and C++ via [Coveralls](https://github.com/alexhagiopol/cracking-the-coding-interview/blob/master/Coveralls.io).
6. Adding prose solution descriptions for problems that don’t have them.

If you have any contributions, please make a PR to the `master` branch. Feel free to message me for clarification on any of the above items.

### Appendix: Windows Development

On my own Windows machine, I develop using [CLion](https://www.jetbrains.com/clion/) running natively in Windows and test the code using `make test` in a WSL terminal window. For users who do not want to use WSL, I have developed the build and test methodology below:

#### Building

The project can be developed purely on Windows without WSL by using Visual Studio 2017. First clone the code:

```auto
git clone https://github.com/alexhagiopol/cracking-the-coding-interview.git
cd ctci
git submodule update --init

```

Then, install Python3 and numpy using your method of choice. I prefer installing Python3 and pip3 from the [official website](https://www.python.org/downloads/) then installing numpy via pip:

```auto
pip install numpy

```

Then, open the project in Visual Studio 2017 which has CMake support. I’ve found that best the workflow is to use Visual Studio to edit code and use debugging functionality. Before building the code, you must follow [these instructions](https://stackoverflow.com/a/12954908) to tell Visual Studio to download PDBs it needs. Additionally, [this article](https://blogs.msdn.microsoft.com/vcblog/2016/10/05/cmake-support-in-visual-studio/) describes Visual Studio CMake support in more detail if you’re interested. A CMakeSettings.json file in the root project directory sets the location of the Visual Studio build folder to be inside the root folder of the project.

#### Testing

Running the tests and seeing their output is best done from a PowerShell window since VisualStudio in CMake mode does _not_ allow the console output of the `tests.exe` and `tests.py` files to remain visible even when pressing CTRL + F5 as described [in this post](https://stackoverflow.com/a/1775870) which applies to “normal” Visual Studio use. I am monitoring the situation re: Visual Studio, and when I find better solutions I will implement them or post them. If you know of something better, please contact me.

In the meantime, from the project root directory, you can run the Python tests using …

```auto
python tests.py

```

… and you can run the C++ tests using …

```auto
.\build\x64-Debug\tests.exe

```

[GitHub Repo](https://github.com/alexhagiopol/cracking-the-coding-interview)

**Happy learning!**

> Thanks to Chief @Aina for assigning this post to me!
