Amazon Interview Question


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
4
of 6 vote

You just need to find all variables and create the dependency graph for them. Then do topological sorting(if this is DAG; if not, find SCCs). Then calculate the values based on the order given the topological sorting.

- Daniel February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Question: What data structures will you use to solve and justify it?

- qstforums February 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Graph represented by ajacency list

- S.Abakumoff February 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

With topological sorting we can't get advantage of multithreading.
I think it's better use a Trie based on arguments
O(ln(k) * k)
k - Cardinality of the set of variables.

- avalter February 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It does not make sense if it is not a DAG. Having a cycle in such a graph means that you cannot schedule it all (example, if a depends on b, b depends on c and c depends on a, you cannot execute any). So it has to be a DAG. And once you have topologically sorted it, determine which functions can be executed simultaneously and then accordingly create threads.

- Anonymous February 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

Simple List or array will do for holding the functions. Use set or hashset for the params since the order is not important but we will have to do contains.
///
Function[] functions = {
new Function("k", "f0", paramSet(new String[]{"a","c","e"})),
new Function("d", "f4", paramSet(new String[0])),
new Function("e", "f1", paramSet(new String[]{"a", "b", "c","d"})),
new Function("c", "f2", paramSet(new String[]{"a", "b", "d"})),
new Function(null, "f3", paramSet(new String[]{"a","c"})),

};
Arrays.sort(functions,
new Comparator<Function>() {

@Override
public int compare(Function o1, Function o2) {
if (o1.params.contains(o2.returnVar))
return 1;
if (o2.params.contains(o1.returnVar))
return -1;
if (o1.returnVar == null && o2.returnVar != null)
return 1;

return 0;
}

});
\\\

- bhantol February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how do you handle the next case:
d = f1(c)
c = f2(b)
b = f3(a)
------
input: a

- avalter February 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

adjacency list, since it's a directed graph.

- USTChucat February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Could you please be more specific about what is actually been asked? I mean, by "Now read the file and we need to determine the order of execution of function depending on the input parameter list" what exactly do you mean? Are we here first choose those functions whose parameter list is a subset of the given parameters (I doubt this is what you mean), or what?

- Zhi Qiu February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How about a makefile?

That the build of the left hand side object depends on the build of the right hand side object
a: b
c: d
b: e
e: f

and make must find the least dependent object to build it and also determine the build sequence

- USTChucat February 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

updated question

- qstforums February 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks for both explanations. I see, now I understand the suggestion based on performing a topological sort. But could you please elaborate more on using "adjacency list"? Thanks in advance.

- Chih.Chiu.19 February 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I was asked this question by the interviewer that which kind of data structure you are going to represent the objects and dependency relations in makefile....

- USTChucat February 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I see, thanks. Use adjacency list to represent the graph then run a topological sort on it, get it. And using adjacency list is better because it is dynamic --- as opposed to using adjacency matrix representation which is better to be pre-allocated, am I right?

- Chih.Chiu.19 February 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think, you would want to create a Directed Acyclic Graph and then apply topological sorting (TS). TS would be actually depth first traversal of the DAG.

Are all method names unique? Are a,b and d method names or symbols?

- nik February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

topological sort

- chiru_chidya March 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

This comment has been deleted.

- Administrator February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

subscribing to the thread

- Nitin Gupta February 07, 2013 | 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