Amazon Interview Question






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

12
concatinate(1,2)

- fiddler.g July 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please explain your answer

- DashDash July 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

fiddler.g plz explain how that output came.

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

When using the preprocessor operators (#, ##, #@ (only for Microsoft)) in the macro, the arguments are treated as is, that is without trying to search any other macros within it.

- fiddler.g July 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

same2(concatinate(1,2)) --->same1(concatinate(1,2)) ---->same1((1##2)) ->#(12)->"12";

same1(concatinate(1,2))--->#(concatinate(1,2))->"concatinate(1,2)"

Note after “#”“##” is scanned , no macro can be treated as macro, they will be treated as string.

It looks to me like Macro is from left to right BFS instread of DFS

- Charles January 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

wth kind of question is this?

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

Man I've been a developer for 10 years and I've never run into a macro with # in it. I've been on large code bases too.

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

I wonder why amazon even bothers with this question? If the candidate gets it wrong, what does that mean? If he/she gets it right, what does that mean?

Really amazon, get a clue here.. :)

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

I presume you have never worked with the icu set of internationalization libraries.
Yes, this makes the code suck but it is made in such a way for a reason.

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

I had the exact same feelings as above person when I saw this question.
It doesn't tell much at all about the interviewee. Even if s/he gets it wrong:
1) He may just got confused
2) He may just didn't know ## crap
3) He hates macros anyways, so => Make him the director of a S/w org at Amazon LOL..

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

# is used for stringifying
## is used for concatenation

Ordinary arguments to macros are COMPLETELY expanded before stringification is applied.

- anon July 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

answer is 12 in both the cases........thnx

- raj July 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#a means "a"
a##b means ab thus #(1##2)=========>>"12"

- raj July 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

concatinate(1,2)
concatinate(1,2)

- aaaaaaa July 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

$ cat test2.c

# include <stdio.h>

#define concatinate(a,b) a##b
#define same1(a) #a
#define same2(a) same1(a)

int main(){
printf("%s\n",same2(concatinate(1,2)));
printf("%s\n",same1(concatinate(1,2)));
}

$ cc test2.c
$ ./a.out

12
concatinate(1,2)

- initrd August 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

When the compiler comes to this line:
printf("%s\n",same2(concatinate(1,2)));
it sees same2 for which it replaces it with same1.

Now, it sees concatinate(1, 2). It immediately replaces it with 12.

When it comes to same1, it sees a definition and replaces the rest of the code with #a i.e. concatinate(1, 2).

So, the compiler actually does the same2 in pass two. And same1 in pass one.

- Sledge Hammer September 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The answer is by the way:
12
concatinate(1,2)

- Sledge Hammer September 26, 2010 | Flag


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