Apple Interview Question for Software Engineer / Developers


Team: Data Mining
Country: United States




Comment hidden because of low score. Click to expand.
3
of 5 vote

I think this is the way easier way to tell if the stack is growing upwards or downwards...
Idea--
1. I am declaring a variable in main function and storing it's address
2. Then i call a function from main and declare a variable there store this address in another variable.
3. Based on the values of addresses i can easily tell whether the stack is growing upwards or downwards.

#include<stdio.h>		
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<unistd.h>


int func()
{
	int p=0;
	return (&p);
}
int main()
{
	int i=9;
	int x=&i;
	int y=func();
	if(x>y)
		printf("Downward\n");
	else
		printf("Upward\n");
       return 0;
}

- Best Answer April 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I dont think you can return the address of the local variable. That variable will be popped out and the memory location will be freed..

- shraddha May 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Doesn't matter if that part of the stack is popped. It's still going to return the location of where the data used to be. And that's all we care about.

- KachikosBodkin February 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

stack and heap are memory used for allocation by programs. A program has:
1. code
2. heap
3. stack
4. BSS
5. data (global and static data)

stack is used for storage of auto variables in program. When a program starts in main() all the auto variables are stored in stack using LIFO algo. Stack can grow down or UP depending on the system being used.

example code taken from other site :)

debug this program and see how memory is being allocated, this will give you a fare understanding of how stack works.

This program will give error, because in "fn()" we eat up the memory assigned to "i" and when fn() finishes that memory is freed. But we try to print "i' which has already been freed.

#include <iostream>
using namespace std;

void fn(void)
{
    int * ptr;
    ptr = (int *)&ptr;
    ++ptr;
    int* mainfp = (int*)(*ptr);
    --mainfp;
    --mainfp;
     *mainfp = 20;
    return;
  }

int main (void)
{
   int i = 10;
   fn();
   cout<<”I =<<i<<endl;
   return 0;
}

- Nik April 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thank you.

- apple-maybe? April 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's incorrect. You will get an exception inside fn, because you're trying to write to a location that's out of bounds.

- KachikosBodkin February 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<stdlib.h>
#include<stdio.h>

int *recursive(int x){
        int y=0;
        if(x==1)
            return &y;
        int *res = recursive(x-1);
        if((res-&y)>0)

printf("Stack is growing towards higher address\n");
        else
        printf("Stack is growing towards lower address\n");                             }

int main(){
        recursive(3);
}

- prince April 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void function f()
{
int p=0, q=0;
int *add_p =NULL, *add_q=NULL;

add_p = &p;
add_q = &q;

if(add_p > add_q) {
/* p higher on stack */
printf("Downward growing");
} else {
printf("Upward growing");
}

- Anonymous August 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This might not be correct. because of optimization the local variables inside a function may be reordered on the stack.

- Rayden October 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdlib.h>
#include<stdio.h>

int *recursive(int x){
int y=0;
if(x==1)
return &y;
int *res = recursive(x-1);
if((res-&y)>0)

printf("Stack is growing towards higher address\n");
else
printf("Stack is growing towards lower address\n");
}

int main(){
recursive(3);
}

- Anonymous April 20, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More