Adobe Interview Question for Computer Scientists






Comment hidden because of low score. Click to expand.
1
of 1 vote

int sprintf ( char * buffer, const char * format [ , argument , ...] );
#define is handled by preprocessor, typedef by compiler
#define has no scope control
some things cannot be done by #define like intelligent declaration of pointers.
typedef int* int_p1;
int_p1 a, b, c; // a, b, and c are all int pointers.

#define int_p2 int*
int_p2 a, b, c; // only the first is a pointer!

- aditya.bokaro August 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

#define swap(t, x, y) { \
                                        t _z; \
                                        _z = x; \
                                        x = y; \
                                        y = _z; \
                                    }

1. Generic macro for swapping values of any POD type
2. Only glitch is, it won't work if one of the arguments is _z

- mag February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

macro for swapping two values of the same type:

#define swapp(a,b) a=((a+b)-(b=a))

- Vip June 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your code doesnt work if the variables are not of integer types.

#define swap(type,a,b){type c = a; a = b; b=c;}

We can use this macro and pass the type as an argument.
e.g. swap(int,a,b) will swap two integers a and b

- kkishore June 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you can also do this if you don't want to have to pass the type:

#define swap(a,b){if (!a) { a = b; b = 0;} else if (!b) {b = a; a = 0;} else { a = a^b; b= a^b; a = a^b;}}

- Vanja July 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually, my method does not work with structs. It would work with pointers and any other primitive. However, kkishore's version does give you type safety, so it's probably the best.

- Vanja July 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

#define SWAP(x, y) {typeof(x) temp##x##y = x; x = y; y = temp##x##y;}

- Shiny April 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

3)how free remembers how many bytes to delete

Its implementation dependent. In Linux, malloc allocates 4 bbytes more than the actual allocation size. These 4 bytes are 4 bytes before the allocated pointer. And they store the amount of memory allocated by malloc. So when free(ptr) is called, the compiler goes 4 bytes before ptr and reads the amount to be deleted.
This could be totally different for other OSs. Something like maintaining a table of allocations with pointer allocated as key by the heap manager. Or storing the number of bytes allocated as the first few bytes of allocation. HTH

- Abhinay June 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

typedef is interpreted by the compiler whereas define is not .

- Seth June 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

When you call malloc(), you specify the amount of memory to allocate. The amount of memory actually used is slightly more than this, and includes extra information that records (at least) how big the block is. You can't (reliably) access that other information - and nor should you :-).

When you call free(), it simply looks at the extra information to find out how big the block is.
typically this is stored at the end of the block but is highly machine dependent as in some machines this is mapped at some other location.

- aditya.bokaro August 08, 2011 | 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