C++ brainteasers - Have fun and learn. You may get a job too

Joined
5/2/06
Messages
12,112
Points
273
To quote Mr. Connor on his experience interviewing C++ programmers for FE jobs
In recent times, I've handed over a laptop and asked a candidate to write some code for a standard algorithm like quicksort or even just a hash table. In fact, I've found the best predictor of programming skill is actually the expression on their face when I do.
So I thought the best way to learn is play. So it may be fun and beneficial to post programming problems I encouter on various forum, brainteaser books. I do not know the answers before hand. If you see some nontrivial programming questions, please post here. You may very well see them during your interviews.
Code:
1) Write one line of code to swap the contents two variables without using a temp variable
2) Write a program to print 1-100 and backward without using loops.
3) Write a C++ program to print out "Hello world" without using any ';'
 
Re: C++ puzzles - Have fun and learn. You may get a job too

Andy said:
To quote Mr. Connor on his experience interviewing C++ programmers for FE jobs
In recent times, I've handed over a laptop and asked a candidate to write some code for a standard algorithm like quicksort or even just a hash table. In fact, I've found the best predictor of programming skill is actually the expression on their face when I do.
So I thought the best way to learn is play. So it may be fun and beneficial to post programming problems I encouter on various forum, brainteaser books. I do not know the answers before hand. If you see some nontrivial programming questions, please post here. You may very well see them during your interviews.
Code:
1) Write one line of code to swap the contents two variables without using a temp variable
2) Write a program to print 1-100 and backward . . . without using loops.

These are not difficult but a person with no programming knowledge might struggle. These are my answers. I haven't tried them but they should work.
1)
Code:
#include <algorithm>
...
   std::swap(a, b);
...

2) This is a little more conversome but it's also straight forward:
Code:
#include <iostream>
...
void printForward(int i, int max)
{
    if (i > max) {
        return;
    } else {
        std::cout << i << std::endl;
        printForward(i + 1, max);
    }
}

void printBackward(int i, int min)
{
    if (i < min) {
        return;
    } else {
        std::cout << i << std::endl;
        printForward(i - 1, max);
    }
}

int main()
{
    printForward(1, 100);
    printBackward(100, 1);
    return 0;
}
 
Here is C++ style code to print 0 - 100 and then 100-0 using one function without loops
Code:
#include <iostream>  
void count(int n, int max)
{
std::cout <<n<<" ";
if(n < max) count(n+1, max);
std::cout << n<<" ";
}
int main()
{
int n = 100;
count(0, n);
}
Code:
C:\Fall 2006\9815>swap.exe
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 8
3 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 100 99 98 97 96 95 94 93 9
2 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66
65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39
 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 1
2 11 10 9 8 7 6 5 4 3 2 1 0
 
Here's one that gets a few people:

1: Write a function to compute factorial using recursion.
2: Do the same for Fibonacci numbers.

What's basically wrong with the Fibonacci code ?
Almost everyone makes the same mistake.
 
DominiConnor said:
Here's one that gets a few people:

1: Write a function to compute factorial using recursion.
2: Do the same for Fibonacci numbers.

What's basically wrong with the Fibonacci code ?
Almost everyone makes the same mistake.
Code:
double factorial(double n)
{
    if (n == 0.) return 1.
    return n * factorial(n-1);
}
// I think this should work - haven't tested it
double fibonacci_aux(double n, double f0, double f1)
{
     if (n == 0) return f1;
     return fibonacci_aux(n-1., f1, f0 + f1);
}
double fibonacci(double n)
{
    if (n == 0) return 1.;
    return fibonacci_aux(n, 0, 1);
}
 
Not a c++ programmer but doesnt fibonacci using recursion take too long? I mean if you want the 1000th fib number it can take some time. 4 mins?
 
Code:
1) Write one line of code to swap the contents two variables without using a temp variable
Here is my one line swapping
Code:
a-=b=(a+=b)-b; //one line swapping using +/-
Same idea but will not work if one of the numbers is 0 since it involves multiplication and division
Code:
a/=b=(a*=b)/b;//one line swapping using */:
here is my test code and output
Code:
#include <iostream>
int main(){
    double a,b;
    std::cout <<"Input a,b separated by space : ";
    std::cin >> a >>b;
    a-=b=(a+=b)-b; //one line swapping +/-
    std::cout <<"a= "<< a <<", b= "<< b<<std::endl;
}
Code:
Input a,b separated by space : 4.5 -6
a= -6, b= 4.5
 
RussianMike said:
Not a c++ programmer but doesnt fibonacci using recursion take too long? I mean if you want the 1000th fib number it can take some time. 4 mins?
That's not totally true. A good compiler will unwind the tail recurssion but that's another discussion.
 
lol. when I was taking c++ class to learn it, the teacher was trying to use recursion to find 1000th fib number and it was taking a long time. He was using visual C++.
 
1) Write one line of code to swap the contents two variables without using a temp variable
Code:
void swap(int &a, int &b) {
         a^=(b^=(a^=b));
}
 
I know this thread is a bit old, but hereis what I came up with for the "Hello, world" with no semicolon:
Code:
void main()
{
if(printf("Hello World!\n")){}
}
 
Factorial calculated during compilation:

Code:
#include <iostream>

template<unsigned n>
struct factorial
{
    enum { value = n*factorial<n-1>::value };
};

template<>
struct factorial<0>
{
    enum { value = 1 };
};

int _tmain(int argc, _TCHAR* argv[])
{
    std::cout << factorial<10>::value << std::endl;
    return 0;
}
 
Code:
// Fibonacci.cpp
//
// Fibonacci numbers using templates.
//
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 
// 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 
// 121393, ...[1] 
//
// DJD
#include<iostream>
usingnamespace std;
template <unsigned N>
struct Fibonacci
{
enum { value = Fibonacci<N-1>::value + Fibonacci<N-2>::value};
};
template <>
struct Fibonacci<0>
{
enum { value = 0 };
};
template <>
struct Fibonacci<1>
{
enum { value = 1 };
};
int main()
{
cout << Fibonacci<21>::value << endl;
return 0;
}
 
Very cheeky, but works user-side! :)
Code:
//Swap two numbers without temp
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
cout<<"a="<<endl;
cin>>a;
cout<<"b="<<endl;
cin>>b;
cout<<"a="<<b<<endl;
cout<<"b="<<a<<endl;
getch();
}

This is another one. I'm trying to do this without using any built-in functions or anything. This one requires a bit of work from the user.
Code:
//Swap numbers without temp variable
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b, i;
cout<<"a="<<endl;
cin>>a;
cout<<"b="<<endl;
cin>>b;

if (a<b)
{
for(i=a;i<b;i++)
{
++a;
}
cout<<"--------->a="<<a<<"<---------"<<endl;
}
else if(a>b)
{
for(i=a;i>b;i--)
{
--a;
}
cout<<"--------->a="<<a<<"<---------"<<endl;
}
cout<<"Enter the two numbers again!"<<endl;
cout<<"a="<<endl;
cin>>a;
cout<<"b="<<endl;
cin>>b;

if(b<a)
{
for(i=b;i<a;i++)
{
b++;
}
cout<<"--------->b="<<b<<"<---------"<<endl;
}
else if(b>a)
{
for(i=b;i>a;i--)
{
b--;
}
cout<<"--------->b="<<b<<"<---------"<<endl;
}
getch();
}
/*OUTPUT
a=
12
b=
16
--------->a=16<---------
Enter the two numbers again!
a=
12
b=
16
--------->b=12<---------
*/

Yep...cheekier!
 
Last edited:
Re: C++ puzzles - Have fun and learn. You may get a job too



These are not difficult but a person with no programming knowledge might struggle. These are my answers. I haven't tried them but they should work.
1)
Code:
#include <algorithm>
...
   std::swap(a, b);
...

2) This is a little more conversome but it's also straight forward:
Code:
#include <iostream>
...
void printForward(int i, int max)
{
    if (i > max) {
        return;
    } else {
        std::cout << i << std::endl;
        printForward(i + 1, max);
    }
}

void printBackward(int i, int min)
{
    if (i < min) {
        return;
    } else {
        std::cout << i << std::endl;
        printForward(i - 1, max);
    }
}

int main()
{
    printForward(1, 100);
    printBackward(100, 1);
    return 0;
}

I did the following for number 1:

void swap (int& x, int& y)
{
x=x+y;
y = x- y;
x = x -y;
}
 
#include<iostream>
using namespace std;
int main()
{
static int a = 1;
static int b = 0;
if(b == 0)
{
if(a <= 100)
{
cout<<a++<<" ";
main();
}
}
if(a >= 100)
{
b = 1;
}
if(b == 1)
{
if(a > 1)
{
cout<<--a<<" ";
main();
}
}
}
 
#include<iostream>
using namespace std;
int main()
{
cin>>a>>b;
a = b + a - (b = a);
cout<<a<<" "<<b;
}
 
#include<iostream>
using namespace std;
int main()
{
static int a = 1;
static int b = 0;
if(b == 0)
{
if(a <= 100)
{
cout<<a++<<" ";
main();
}
}
if(a >= 100)
{
b = 1;
}
if(b == 1)
{
if(a > 1)
{
cout<<--a<<" ";
main();
}
}
}
What's the question for this C code?

BTW static variables are considered harmful.
 
Back
Top