There was an interview question a while back, which got me confused for a while and at the moment.
Create a function such that Every time I call it, it must return an incremented value by 1(start from 0), until it reaches a certain value k, then decrease till it reaches 0.
No passing of arguments(Constraint).
I gave a straightforward solution:
Declare a global variable which increments each time the function is called until it reaches k, then decrement it.
Then they modified the question and added that, after they call the function, they call another function which changes the value of the global variable. Now what.
So I simply said, we store the value elsewhere, but again they said what if the stored value is manipulated again... Any solutions guys ??
So basically, a function that returns an incremented value each time it's called until it reaches k, then decreases,can't use arguments, and can't store the result somewhere.
#include <iostream>
int function()
{
// Let's say k =12, because I need it to be an actual number
staticbool ascending = true;
staticint value = 0;
int returnValue = value;
if (value == 12)
{
ascending = false;
}
if (ascending) { value++;}
else {value--;}
return returnValue;
}
int main()
{
for (int i = 0; i < 25; ++i)
{
std::cout << function() << ' ';
}
}
If it can't use arguments, it has to store its state somewhere, because it isn't a pure function.
If the interviewers didn't like the use of a global variable, the two other alternatives are to use class-scope variables, and have the function be a member function of a class, or to use static variables within the function.
It's unclear what you want the initial return value of the function to be, but here's two possible implementations
Wouldnt recursion work? Keep global variable K reference thats the number you want to go up to in the recursive function and when you hit that number recursive go to 0 in the same function. Even if the global variable is changed after the function is called the recursion would have to finish before any other function was called since a recursive function calls itself until the base case is reached.
No,we need the value at the moment of calling the function, we are not printing it..
So we need the value to be returned after calling it. When I heard the question recursion was my first guess also, but I cleared this with the interviewer and he said Don't use recursion here.
I never said to print the number and you would have the value at the moment of calling... but if the interviewer said not to use recursion then guess it doesnt work.
It's not periodic then :( but being periodic wasn't explicitly mentioned in the OP, so yeah that's fine.
Just a general note to Noonoob... watch out for any function that has non-const variables with static duration (or globals) if you ever go into a multi-threaded process. You don't want race conditions. https://en.wikipedia.org/wiki/Race_condition
It's not periodic then :( but being periodic wasn't explicitly mentioned in the OP, so yeah that's fine.
It wasn't clear what was supposed to happen when 0 is reached again. I like making the function periodic and it's easy to modify my solution to be so with modular arithmetic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
constint k = 5;
int foo()
{
staticint counter = -1;
return ((++counter)%(2*k) <= k ? counter%(2*k) : 2*k-(counter)%(2*k));
}
int main()
{
for (int i = 0; i <= 8*k; i++)
std::cout << foo() << '\n';
return 0;
}
Edit: Grime,
There's a few differences between global, global static, and local static variables.
A local static variable is, of course, only accessible within the function. The first time the function is called, the local static variable will be initialized, and will stay initialized in subsequent calls.
A global variable (both static and not static) is initialized before main begins (will be initialized to 0 if not specified, unlike non-static local variables).
Also, a static variable is only visible to the translation unit it's defined it (i.e. no external linkage).
In general (not just talking about global/static variables), create things close to where they are used so as to avoid confusion about the state of something. More state = more things the programmer has to keep track of as they work = more chance of the programmer forgetting something = bug.