Skip to content

Instantly share code, notes, and snippets.

@RhysU
Last active December 16, 2015 10:29
Show Gist options
  • Save RhysU/5420724 to your computer and use it in GitHub Desktop.
Save RhysU/5420724 to your computer and use it in GitHub Desktop.
How not to land a job.
// Whatever you do, when asked to enumerate permutations of length n,
// don't demonstrate that you can quickly produce this solution...
#include <algorithm>
#include <cerrno>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <numeric>
#include <stdexcept>
#include <vector>
int main(int argc, char *argv[])
{
using namespace std;
// Parse incoming single command line argument
if (argc != 2) throw invalid_argument("argc != 2");
errno = 0;
char *end;
const unsigned long n = strtoul(argv[1], &end, 10);
if (errno || end == argv[1]) throw invalid_argument(argv[1]);
// Output the requested permutations
vector<unsigned long> s(n, 1);
partial_sum(s.begin(), s.end(), s.begin());
do {
copy(s.begin(), s.end(), ostream_iterator<unsigned long>(cout, " "));
cout << '\n';
} while (next_permutation(s.begin(), s.end()));
return EXIT_SUCCESS;
}
// ...but instead mumble briefly about how next_permutation works,
// worry about the algorithmic internals of std::next_permutation for 10
// minutes in front of a blank whiteboard. Ultimately produce nothing. And
// then ask the interviewer to please move on.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment