qstforums
BAN USER- 0of 0 votes
AnswersGiven a file which contains list of function with parameters and return types (Assume parameters and return type as primitives) they can be of any order
- qstforums in United States
Example: File functions.txt contains below info
int e=f1(a,b,c,d)
int c=f2(a,b,d)
f3(a,c)
float d = f4()
question follows as below
We need to implement function
execute(functions.txt,a,b,d) where functions.txt is the file which contains just functions info and a,b, d are input parameters of functions defined in the file.
Now read the file and we need to determine the order of execution of function depending on the input parameter list
we can't execute f1 first because we have only parameters a , b and d
f2 can be executed because we have a, b and d parameters, which gives parameter c
now f3 can be executed because we have parameters a and c..
so we need to find the order and execute functions as quickly as possible (can use multi threading)
so efficient order is
f2 and f4 can run parallel and then f1 and f3 can be started only after completion of f2.
What are the data structures we use here to solve this problem
Consider execute function which takes always 1st parameter as filename and the rest of parameters as dynamic (similar to ... in java for dynamic parameter list)| Report Duplicate | Flag | PURGE
Amazon Data Structures - 0of 0 votes
Answersfind the distance between 2 values in a binary search tree. Node will have value, left node and right node
implement the function int Distance(Node root, int val1, int val2) without recursionint Distance(Node root, int val1, int val2) { if(val1== val2) { return 0; } while((root.val<val1 && root.val <val2) || (root.val >val1 && root.val2 >val2)) { if(val1 < root.val) { root = root.left; } else { root = root.right; } } return depth(root, val1) + depth(root,val2) }
Note: function depth(root, val) returns the depth of sub tree. Interviewer is ok about the depth of tree functionality but he asked me if Distance(Node root, int val1, int val2) implementation is correct.
- qstforums in United States
Complexity is O(logN)| Report Duplicate | Flag | PURGE
Amazon Algorithm
updated question
- qstforums February 07, 2013