Adobe Interview Question for Software Engineer / Developers






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

There is no good answer to this question. If the values are integers, a well-known trick using exclusive-OR could perhaps be used, but it will not work for floating-point values or pointers, or if the two values are the same variable. If the macro is intended to be used on values of arbitrary type (the usual goal), any solution involving a temporary variable is problematical, because:
* It's hard to give the temporary a name that won't clash with anything. (Any name you pick might be the actual name of one of the variables being swapped. If you tried using ## to concatenate the names of the two actual arguments, to ensure that it won't match either one, it might still not be unique if the concatenated name is longer than 31 characters, and it wouldn't let you swap things like a[i] that aren't simple identifiers. You could probably get away with using a name like _tmp in the ``no man's land'' between the user and implementation namespaces;
* Either it can't be declared with the right type (because standard C does not provide a typeof operator), or (if it copies objects byte-by-byte, perhaps with memcpy, to a temporary array sized with sizeof) the macro can't be used on operands which are declared register.
The best all-around solution is probably to forget about using a macro, unless you're willing to pass in the type as a third argument. (Also, if you're trying to swap entire structures or arrays, you probably want to exchange pointers instead.) If you're worried about the use of an ugly temporary, and know that your machine provides an efficient exchange instruction, convince your compiler vendor to recognize the standard three-assignment swap idiom in the optimization phase.
If you're consumed by a passionate desire to solve this problem once and for all, please reconsider; there are better problems worthier of your energies.

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

xcc

- Anonymous September 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I disagree with #1, because you can easily get around with name clash by defining a scope using '{' and '}', e.g.,

#define SWAP(a,b,type) { type tmp=(a); (a)=(b); (b)=tmp; }

w.r.t, #2 you can let the user pass in the type as shown above.

This code works as least well for most types in C, except when you want deep copy with pointers, which is another story BTW.

- Anonymous October 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define Swap(T, a, b) {T temp = a; a=b; b=temp;}

- Anonymous September 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

General needs a definition; this might be a class object as well.
Use templates if that's true.

else if normal built in datatypes that XOR is fine.
#define SWAP(a,b) {a=a^b;b = a^b;a= a^b;}

Akash
tech-queries.blogspot.com

- Akash September 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not correct for float.If you not sure please do not post or check it before posting.

- Anonymous September 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the idea is to have function arguments cast to void pointer.

- Messi September 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define Swap(X,Y) { __typeof__ (X) _T = X; X = Y; Y = _T; }

- Anonymous September 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If I recall correctly, __typeof__ is a GNU C extension. Using this won't be portable.

- Anonymous October 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi All,
The idea here is to pass the parameters as void parameters as well as the size of the data type that needs to be swapped. Then we need to use memcpy with a temporary void pointer. here is how a code might look like.

void swap(void* a, void* b, int size){
void * temp =NULL;
memcpy(temp, a, size); //copy size number of bytes starting from a to temp
memcpy(a, b, size); //copy size number of bytes starting from b to
memcpy(b, temp, size); //copy size number of bytes starting from temp to b
}

Please let me know if this is correct or needs some manipulations.

- Soma September 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

your answer seems very effective...but we had to write a macro if suppose i write this as macro then will this be okay??

#define SWAP(x,y) { void *temp=null; void *a; void *b; a=&x; b=&y; memcpy(temp,a,sizeof(x)); memcpy(a,b,sizeof(y)); memcpy(b,temp,sizeof(x));}

- sweetest September 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

wondering how you are able to copy to a NULL pointer.this solution will result in seg. fault.

- Anonymous October 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

void *temp= malloc (sizeof(x)); should do it.
Have tested it works fine

- Anonymous October 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

should use memmove instead of memcopy to consider memory overlapping conditions

- fabregas March 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

and

- Anonymous September 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

# define swap(x,y) /
{ void *_tmp=malloc(sizeof(x)); /
void *_x=&x; void *_y=&y; /
memcpy(tmp,_x,sizeof(x));/
memcpy(_x,_y,sizeof(x));/
memcpy(_y,_tmp,sizeof(x));}

and in GCC we can do :

#define swap(x,y) {typeof(x) _tmp=x,x=y,y=_tmp}

- ridercoder October 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

second memcpy should have sizeof(y) instead of sizeof(x)

- Anonymous November 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

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

int main(void)
{
int a = 10000, b = 2;
float x = 99999.0f, y = 2.0f;
int *pa = &a;
int *pb = &b;

printf("BEFORE:\n");
printf("a = %d, b = %d\n", a, b);
printf("x = %f, y = %f\n", x, y);
printf("pa = %p, pb = %p\n", pa, pb);

SWAP(a, b); // swap ints
SWAP(x, y); // swap floats
SWAP(pa, pb); // swap pointers

printf("AFTER:\n");
printf("a = %d, b = %d\n", a, b);
printf("x = %f, y = %f\n", x, y);
printf("pa = %p, pb = %p\n", pa, pb);

return 0;
}


Hope this code is easily understandable and works for all possible cases...

- Anu December 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The loop is unnecessary, this works and is a little more clear

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

- Anonymous June 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the code form swapping using generic macro...pertaining to this qs all the scepticism shown by many is disapproved.

#include<stdio.h>
#define swap(A,B) A=A+B;B=A-B;A=A-B;
void main()
{
char a='c',b='d';
//int p=5,q=10;
//swap(p,q);
//float r=5.0,s=10.5;
//swap(r,s);
//printf("p=%d\tq=%d\n",p,q);
//printf("r=%f\ts=%f\n",r,s);
//swap(a,b);
//printf("a=%c\tb=%c\n",a,b);
char *ptr1,*ptr2;
ptr1=&a;ptr2=&b;
swap(*ptr1,*ptr2);
printf("a=%c\tb=%c\n",*ptr1,*ptr2);
}

- ashish January 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can use function template

template <class t>
void swap(t a,t b)
{
t temp=a;
a=b;
b=temp;
}

- Learn Android: http://learnandroideasily.blogspot.in/ June 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define swap(type,a,b) {type c=a;a=b;b=c;}
njoy :P best of the best ...4 every case ..

- aditya rajhans October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*so simple*/
scanf("%d%d",&x,&y);
x=x^y;
y=x^y;
x=x^y;
printf("%d"\n%d",x,y);

- avinash September 22, 2012 | 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