1.2. Tools and Environment Setup

The purpose of a programming language is to help express ideas in code.

—Bjarne Stroustrup

1.2.1. Operating System

All the examples in this book works in Ubuntu 20.04.4 LTS.

$ lsb_release -a
No LSB modules are available.
Distributor ID:       Ubuntu
Description:  Ubuntu 20.04.4 LTS
Release:      20.04
Codename:     focal

1.2.2. C++ Standard and Compiler

In the past 10 years, C++ has drmatically changed. The C++ standard has evolved from C++98 to C++20 in order to embrace more modern programming language features. We will use C++20 as the standard for this book. However, in the interview, the focus is on data structure and algorithm. Unless there is a special requirement for candidates’ C++ familiarity, C++98 is enough for the candidate to do the work.

GNU g++, LLVM clang++, Microsoft Visual C++ are popular C++ compilers. In this book, clang++ 15.0.0 is used though it is still under development at the time of this writing [1].

$ clang++ --version
Ubuntu clang version 15.0.0-++20220225053013+37e84d9be06d-1~exp1~20220225173121.170
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

To install clang 15, add the two lines in /etc/apt/sources.list:

deb http://apt.llvm.org/focal/ llvm-toolchain-focal main
deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal main

Add gpg key to retrieve the archive signature:

wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -

Then use apt to update and install all necessary packages:

sudo apt update
sudo apt install clang lld lldb clang-format clang-tidy -y

Not all the language and library features in C++ standard are supported by compilers.

Footnotes

1.2.3. C++ REPL

C++ is a static language, in contrast with dynamic langues like Python. This makes it easy to write high performance program but sometimes hard to learn. Fortunately there is a REPL(read–eval–print loop) tool cling to help. A read–eval–print loop (REPL), also termed an interactive toplevel or language shell, is a simple interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user; a program written in a REPL environment is executed piecewise. cling is developed by CERN(European Organization for Nuclear Research) and hosted under its ROOT project, where you can download Linux and Mac version.

At the time of this writing, the latest version of cling is cling_2020-11-05_ROOT-ubuntu2004.tar.bz2. Run the following command to install cling:

wget https://root.cern.ch/download/cling/cling_2020-11-05_ROOT-ubuntu2004.tar.bz2
tar xjvf cling_2020-11-05_ROOT-ubuntu2004.tar.bz2
sudo mv cling_2020-11-05_ROOT-ubuntu2004 /usr/local
export PATH=/usr/local/cling_2020-11-05_ROOT-ubuntu2004/bin:$PATH
exec $SHELL

Now you can run cling in the terminal and try some C++ code.

 1$ cling -std=c++2a
 2
 3****************** CLING ******************
 4* Type C++ code and press enter to run it *
 5*             Type .q to exit             *
 6*******************************************
 7[cling]$ #include <iostream>
 8[cling]$ #include <fstream>
 9[cling]$ #include <cstdlib>
10[cling]$ #include <filesystem>
11[cling]$ namespace fs = std::filesystem;
12[cling]$ fs::current_path(fs::temp_directory_path());
13[cling]$ fs::create_directories("sandbox/1/2/a");
14[cling]$ fs::create_directory("sandbox/1/2/b");
15[cling]$ fs::permissions("sandbox/1/2/b", fs::perms::others_all, fs::perm_options::remove);
16[cling]$ fs::create_directory("sandbox/1/2/c", "sandbox/1/2/b");
17[cling]$ std::system("ls -l sandbox/1/2");
18total 12
19drwxrwxr-x 2 henry henry 4096 Feb 27 10:19 a
20drwxrwx--- 2 henry henry 4096 Feb 27 10:19 b
21drwxrwx--- 2 henry henry 4096 Feb 27 10:19 c
  • Line 1 is to launch cling REPL environment.

  • Line 12 is to change current directory to system’s temporary folder

  • Line 13, 14 and 16 are to create folders

  • Line 15 is to change permissions of one folder

  • Line 17 is to run a system command to print the contents of one folder

The code snippet above shows the new standard does make the programmer life easier. With built-in filesystem library support in modern C++, we don’t have to use third-party library to write cross-platform code.

To save the typing, we can set alias:

alias cling='cling -std=c++2a'

And put all the common C++ header files into one sein.hpp:

#pragma once
#include <iostream>           // Stream
#include <fstream>
#include <vector>             // STL
#include <string>
#include <list>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <forward_list>
#include <algorithm>
#include <functional>
#include <tuple>
#include <deque>
#include <array>
#include <stack>
#include <queue>
#include <bitset>
#include <iterator>
#include <memory>             // Memory Management
#include <chrono>             // Time
#include <cassert>            // Assert
#include <exception>          // Exception
#include <thread>             // Multithread
#include <mutex>
#include <future>
#include <condition_variable>
#include <cmath>              // Numerics
#include <filesystem>         // File System
using namespace std::chrono;
using namespace std;
namespace fs = std::filesystem;

In cling, we can just include sein.hpp and using the classes and functions directly.

 1$ cling
 2[cling]$ #include "sein.hpp"
 3[cling]$ vector<string> v={"1","2"};
 4[cling]$ v.size()
 5(unsigned long) 2
 6[cling]$ v
 7(std::vector<std::string> &) { "1", "2" }
 8[cling]$ string s="hello__world";
 9[cling]$ size_t p= s.find("__");
10[cling]$ p
11(unsigned long) 5
12[cling]$ s.substr(0,5)
13(std::basic_string) "hello"
14[cling]$ s.substr(5+2)
15(std::basic_string) "world"

Note

  • use ‘c++98’ or ‘c++03’ for ‘ISO C++ 1998 with amendments’ standard

  • use ‘gnu++98’ or ‘gnu++03’ for ‘ISO C++ 1998 with amendments and GNU extensions’ standard

  • use ‘c++11’ for ‘ISO C++ 2011 with amendments’ standard

  • use ‘gnu++11’ for ‘ISO C++ 2011 with amendments and GNU extensions’ standard

  • use ‘c++14’ for ‘ISO C++ 2014 with amendments’ standard

  • use ‘gnu++14’ for ‘ISO C++ 2014 with amendments and GNU extensions’ standard

  • use ‘c++17’ for ‘ISO C++ 2017 with amendments’ standard

  • use ‘gnu++17’ for ‘ISO C++ 2017 with amendments and GNU extensions’ standard

  • use ‘c++2a’ for ‘Working draft for ISO C++ 2020’ standard

  • use ‘gnu++2a’ for ‘Working draft for ISO C++ 2020 with GNU extensions’ standard

  • use ‘c++2b’ for ‘Working draft for ISO C++ 2023 DIS’ standard

  • use ‘gnu++2b’ for ‘Working draft for ISO C++ 2023 DIS with GNU extensions’ standard

1.2.4. Advice

C++ is a hard language in terms of coding speed and convenience. As an interviewer, I normally give bonus points to the candidates if they can finish the same coding question with C++ while other candidates use other advanced languages like Python, Java or Go. My interview experience shows that hardcore programmers prefer C++ as their coding languages in the interview, and they normally have very good performance in the interview. It is not because C++ is a better language. It is just because it is hard, as there are not many handy helper functions available in C++. For example, splitting string is a simple task and can be done in Python with one line as below:

1>>> s = "hello__world"
2>>> s.split('__')
3['hello', 'world']

In C++, you have to write a function with many lines. The following is one of the possible implementations with eleven lines of code:

 1vector<string> split(string s, const string& delimiter){
 2  vector<string> r;
 3  size_t pos = 0;
 4  while ((pos = s.find(delimiter)) != string::npos) {
 5      r.push_back(s.substr(0, pos));
 6      s.erase(0, pos + delimiter.size());// or s=s.substr(pos+delimiter.size())
 7  }
 8  if(!s.empty())
 9    r.push_back(s);
10  return r;
11}

To finish the same task, the C++ candidate have to understand the usage of string::find, string::substr, string::erase, string::npos and vector::push_back.

Moreover, C++ requires candidates to consider memory management, which is automatically taken care of by Garbage Collector in advanced languages. So if you want to stand out, be a hardcore programmer and get the bonus points, C++ is a good choice to show you skills and help you crack the data structure and algorithm problems in the competitive interview.

1.2.5. Reference