Speedcoding

Tips on how to solve an interview problem within 20 minutes.

Table of Contents

Writing Code

Builtins

  • __builtin_clz returns the number of leading zeroes.

    • __builtin_ctzl for long.
    • __builtin_ctzll for long long.
  • __builtin_ctz returns the number of trailing zeroes.

    • __builtin_ctzl for long.
    • __builtin_ctzll for long long.
  • __builtin_parity returns 1 if the number is odd, 0 otherwise.

    • __builtin_parityl for long.
    • __builtin_parityll for long long.
  • __builtin_popcount returns the number of ones.

    • __builtin_popcountl for long.
    • __builtin_popcountll for long long.

Also see the Integer Overflow Builtins.

auto Variable Type

auto i = 0;

std Namespace

Avoid typing std:: over and over again.

using namespace std;

Testing Code

Debugging Code

Print the items from a vector:

vector<int> list;

for_each(list.begin(), list.end(), [](auto& item){ cout << item << endl; });

Print the items from an unordered map:

unordered_map<int, int> map;

for_each(map.begin(), map.end(), [](auto& item){ cout << item.first << " " << item.second << endl; });