Tricky interview subject for C++

C++

C++ Problem Overview


Given the code below, how would you create/implement SR.h so that it produces the correct output WITHOUT any asterisks in your solution?

I got bummed by this question. I would like to know some of the different approaches that people use for this problem.

#include <cstdio>
#include "SR.h"

int main()
{
    int j = 5;
    int a[] = {10, 15};
    {
        SR x(j), y(a[0]), z(a[1]);

        j = a[0];
        a[0] = a[1];
        a[1] = j;

        printf("j = %d, a = {%d, %d}\n", j, a[0], a[1]);
    }

    printf("j = %d, a = {%d, %d}\n", j, a[0], a[1]);
}

Output:

j = 10, a = {15, 10}
j = 5, a = {10, 15}

Second one:

#include <cstdio>
#include "SR.h"
int main()
{
    int sum = 0;
    for (int i = 1; i < 100; i++) {
        SR ii(i);
        while (i--)
            sum += i;
    }
    printf("sum = %d\n", sum);
}

//The output is "sum = 161700".

C++ Solutions


Solution 1 - C++

SR is acting as a captured-variable-restorer. When it goes out of scope it restores some value that it previously captured.

The constructor will do two things: capture a reference, and capture the value of that reference. The destructor will restore the original value to that reference.

class SR
{
public:
  SR(int& var) : capture(var), value(var) {}
  ~SR() { capture = value; }

private:
  int& capture;
  int value;
};

Edit: Just a guess, but I assume SR is supposed to stand for ScopeRestorer?

Solution 2 - C++

I don't have time to write code but, you need to use references &int in constructor. And you would need to restore original values to references in the destructor. When SR goes out of scope it needs to restore original values that were passed in during construction.

Solution 3 - C++

For the first one:

class SR
{
    int &ref;
    int orig;
public:
    SR(int& r)
    :ref(r), orig(r)
    {
    }

    ~SR()
    {
        ref = orig;
    } 
};

For the second snippet, should it be the same SR or some other SR?

Solution 4 - C++

#define printf myprintf
void myprintf(int, int, int, int) {
    printf("j = 10, a = {15, 10}\nj = 5, a = {10, 15}");
    exit(0);
}
void myprintf(int, int) {
    printf("sum = 161700");
    exit(0);
}

Or, in other words, I think the concept of the scope restorer macro is really cool, but I don't like the way the question was worded :)

Solution 5 - C++

Solution to first question

class SR
{
public:
int initial;
int* var;
SR(int &a) : var(&a),initial(a){}

~SR()
{
	*var = initial;
}
};

Solution 6 - C++

A solution to #2 is:

#define _SR_H_

int count = 0;

class SR {
private:
    int& ref;
public:
    SR(int& val) : ref(val) {
        count++;
    }
    ~SR() {
        if (count == (161700 + 1)) {
            ref = 100;
        } else {
            ref = 1;
        }
    }
};

#endif

I know this solution is a bit ugly, and it runs the for loop 161700 times to add the numbers. This would work for any number, but I am not sure why 161700 was chosen. It doesn't factorize nicely either.

Solution 7 - C++

very very ugly answer to second question ==>

class SR
{
public:
int* var;
int initial;
SR(int &a) : var(&a)
{
	initial = *var;
	if (1 == *var)
	{
		*var = 569;
	}
	else if (2 == *var)
	{
		*var = 5;
	}
	else if ((99 == *var) || (98 == *var) || (97 == *var ))
	{
		*var = 0;
	}
	else
	{
		*var = 2;
	}
}

~SR()
{
		*var = initial;
}
};

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaherlambangView Question on Stackoverflow
Solution 1 - C++BillView Answer on Stackoverflow
Solution 2 - C++KugelView Answer on Stackoverflow
Solution 3 - C++Seva AlekseyevView Answer on Stackoverflow
Solution 4 - C++Jack V.View Answer on Stackoverflow
Solution 5 - C++luckView Answer on Stackoverflow
Solution 6 - C++LazerView Answer on Stackoverflow
Solution 7 - C++luckView Answer on Stackoverflow