Microsoft Interview Question


Country: India




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

Proposed solution:
1. create a MAP as <function_name_with_signature, booleanFlag>
2. crate a set for private and protected functions.
3. create a MAP for un-known status fuctions i.e function is called or not we dont know..
UN-KNOWN MAP <fucntion_name, list of fucntions called in this function>

Algo:
1.parse every funtion, if it calls any other function, then set flag of the corresponding called function. and add the main function to un-known status fucntion set.

ex: fun1 () { fun2();}

then fun2's flag will be set to true.
and fun1 will be added to UNKNOWN map.

2. for any call to member function in the fucntion definition is found, check in unknown map first, if it is found remove it from UNKNOW.

interviewer is not happy with my answer.......
please post your comments.....I am curious abt answer.

- sse July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sse : Good explanation. However it wont be that simple i think when you are performing parsing. because:

1. There might be different functions in different class hierarchy with same name and one is get called but another wont. For Ex.

class A {
     fun1 ();
  };
 class B {
     fun1 ();
 };

 B b;
 b.fun1 (); //note fun1 is called but you need to determine b's type over here. So you have to maintain symbol table as well

2. functions called with using object but when you call it from class's another member function it wont called using any object (syntactically). For ex.

class A {
     fun1 ();
  };
 class B {
     fun1 ();
    public :
       fun2 () {
            fun1 ()
         }
    };

In above scenario, you need to mark only B's fun1 as known. Also need to maintain per class functions list OR need to perform name mangling.

3. There is function declaration , definition and function call. You need to differentiate all those. It is more difficult when there is no argument. For ex.
//declaration
fun1 ();

//definition
fun1 ()
{
// do relevant stuff
}

//function call
fun1 ()

Most importantly, I think interviewer might have asked to write sudo code or algorithm instead of function/code (question says that write a function).

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

@sachin
great insight view of all points bro..
appreciable

- shreyans July 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

One approach for this is we create the map as it contains all the fucntions which encounter while scanning
starting from main(), the function(s) which is called by main simply put that function into a linked list and remove from map also at the end if map contains any item(s) then those are waste functions and the linked list contains all the used functions

- Vikas Kumar July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Vikas,
in C++ class, there is no need of Main,
even i proposed the same first.

- sse July 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this is similar to code coverage tools. stackoverflow dot com/questions/3051563/how-do-code-coverage-tools-work throws some ligt on this.

- Yoda July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1.Scan the class once to get total number of function definition..let it be n,now create a bool array[n][n],
2-start scanning the class one by one..go through each function definition and make bool value array[i][j]=1 if there is function call from ith function(may 2 function be same)
3-check the functions call from the constructor directly and used the bool array to know other called function..u will in o(n^2) will know which function is not used..and which is used..

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

see if this can work

for every class we do following step's :

1) first we declare a static variable say: static int is_called;
2) now say we have 32 function :
for every function set corresponding bit.
ie. for i'th function include (is_called)&(1<<i)
3) now in class destructor : u can map this variable to know all function called.

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

The question said “Suppose you are developing a code editor” so if this is the case then we can use following path.
Create a list of function also
Create a hashmap which will have function name and a bool parameter say reachable.
So whenever you are writing code and encounter any other function, just add that function to hashmap.

And while compiling the application, return the list of functions which are not present in hashmap.

If code files are already given, then we have to scan the entire code

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

If I take a every function as NODE and make a directed graph through parsing a code ,
and then we can find if there exist a way to reach some function then it is Covered ,
Please comment ..

- Kumar Vishal July 18, 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