How to not land a job.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |