Amazon Interview Question for Software Engineer / Developers






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

This seems like a tree - say a directory tree. As if, 1 is in directory a/b/c, and so on.
So, here's my trick:
1) Create a local string str1 from the tokens on left side. For the first input, the string str1 = "abc", but store it in reverse (for reason explained later). So, str1 = "cba".
2) Whenever you hit "=" in input, go through str1 and create xml structure. So, <a><b><c>1
3) Then go thru next token and store it in reverse in a new string str2. So, str2 = "dba".
4) Now do this: ptr = strstr( str1, str2 ). This is first reason I stored tokens in reverse: to use strstr().
5)
(a) if ptr is NULL, close all xml tokens. That is, write </c></b></a> by looking up str1.
(b) if ptr is non NULL, close only xml tags from str1 to ptr. That is, in current case you'd only close </d>.
6) Assign str2 to str1; read next tag; go back to step 3.

That might be it. Comments?

- Nix October 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Correction: strstr() would in fact work if I "DID NOT" store str1 and str2 in reverse.
So, forget the "reverse" part. Rest of the solution is all the same.
This should work - tiny code.

- Nix October 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can build a trie and do a dfs.Trie in the sense when we see a.b.c=1,create node a ,b,c,and a leaf 1.
Now when we enconter next eq a.b.d=4,nodes a,b already created so it is enough to attach one more child node "d" to node b.
Now after tree is built do a dfs

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

yeah, while doing the dfs, close the tag when a corresponding tag-node is "explored" i.e all its children are visited.

- geek January 05, 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