Bloomberg LP Interview Question
Financial Software Developerstemplate<typename T>
bool isGrowingUp(stack<T>& s) {
T tmp;
s.push(tmp);
T *ptr = &s.top();
s.push(tmp);
bool up = (++ptr == &s.top());
s.pop();
s.pop();
return up;
}
@Nishit, is the following correct then ?
#include <stdio.h>
#include <stdlib.h>
void stackFn(int *var1)
{
int var2;
printf("\n Address of first var = %u ", var1);
printf("\n Address of second var = %u ", &var2);
if(var1 > &var2)
printf("\n Stack is growing downwards.\n");
else
printf("\n Stack is growing upwards.\n");
}
void main()
{
int var1;
stackFn(&var1);
}
stack<int> T;
bool isStackGrowing()
{
static int *ptr = NULL;
if(ptr == NULL)
{
if(T.size() > 0)
{
ptr = &T.top();
return true;
}
else
return false;
}
bool b = ptr < &T.top();
ptr = &T.top();
return (b);
}
T.push(20);
T.push(21);
cout<<"isStackGrowing: "<<isStackGrowing()<<endl;
T.pop();
cout<<"isStackGrowing: "<<isStackGrowing()<<endl;
I was asked the same questions and what I did was to declare 2 variables and print out the address of both and compare as done by Neo, but the interviewer said that there were some times when this approach would not be correct and so I wrote another method which he approved, I declared a variable and assigned a pointer which would hold the address of it, in the next line would call a function and pass that pointer value to it, declare a variable there and compare the address of the new variable and the passed value and thus come to know whether the stack is growing upwards or downwards....
- Nishit September 02, 2009please comment on the solution...