Interview Question for Software Engineer / Developers






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

ve just posted some interveiw questions.Does anyone out there have a clue to the solutions?

- lumsayaw December 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can use two pointers with 5 elements difference in distance.

- Anonymous December 11, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

NODE element_from_end(NODE head,int n)//n is the element from last here it is 5
{
NODE ptr1,ptr2;
ptr1=ptr2=head;
for(int i=0;i<n&&ptr1;i++)
ptr1=ptr1->next;
while(ptr1)
{
ptr1=ptr1->next;
ptr2=ptr2->next;
}
return ptr2;
}

- Ankuj December 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

One problem:
if n is larger than the number of nodes, it will always return the first node.
c++ CODE:
{{

struct Node
{
int d;
Node * next;
};

Node* element_from_end(Node* head,int n)//n is the element from last here it is 5
{
Node* ptr1;
Node* ptr2=0;
if ( n < 1)
{
cout << " n must be larger than 0"<<endl;
return ptr2;
}
ptr1=head;
for(int i=0;i<n;i++)
{

if (ptr1)
{
cout << " i= "<<i <<" d="<< ptr1->d<<endl;
ptr1=ptr1->next;
}
else if (i<n)
{
cout << " it has fewer nodes than "<< n<<endl;
return ptr2;
}

}
ptr2=head;
while(ptr1)
{
ptr1=ptr1->next;
ptr2=ptr2->next;
}
if (ptr2)
cout << " ptr2 "<< ptr2->d<<endl;
else
cout << " null " <<endl;
return ptr2;
}

}}

- kulang December 17, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sample Test Cases:
Make combinations of List and N

List: Empty, 1 element, 10 elements, circular.
N: negative, 0, positive, 1, 2, 9, 10, 11.

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

Node *ReturnNthNode(Node *phead, int n)
{
	Node *pCur = phead;
	
	for(int i=1; i < n && pCur; i++)
	    pCur = pCur->next;

	if(NULL == pCur)
	{
	    return NULL;
	}

	while(pCur->next)
	{
	    pCur = pCur->next;
	    phead = phead->next;
	}

	return phead;
}

- Calista December 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

correction
Node *ReturnNthNode(Node *phead, int n)
{
	Node *pCur = phead;
	// correction
	if(null == phead || 0 >= n)
	{
		return null;
	}

	for(int i=1; i < n && pCur; i++)
		pCur = pCur->next;

	if(null == pCur)
	{
		return null;
	}

	while(pCur->next)
	{
		pCur = pCur->next;
		phead = phead->next;
	}

	return phead;
}

- Calista December 19, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int nthNodeFinder(int n, node inPtr) {
node *front, *nthBack;
int ctr = 0;

front = nthBack = inPtr;

while (front != null && ctr++ < n) {
front = front->next;
}

if (ctr <= n)
return;

while (front != null) {
nthBack = nthBack->next;
front = front->next;
}

return nthBack;
}


table of test cases


test case #, LL Length, N
1 | 0 | 4
2 | 1 | 4
3 | 1 | 1
4 | 3 | 2
5 | 12 | 5
6 | 9 | 0

- shassant2 December 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your code would fail if asked for the nth value in the list of n nodes. Figure out why ;)

- Calista December 19, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

node* getNodeFromLast(node *root,int m){
      int ckt = 0;
      node *element = NULL;
      node *first = root;
      while(root != NULL){
                 root = root->next;
                 ckt+=1;
                 if(ckt == m){
                        element = first;
                 }
                 if(ckt>m){
                           element = element->next;
                 }
      }
      return element;

}

- Anonymous December 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We could use recursion
In the recursive function call the next node till the end of the list
When the stack is unwinding, use a counter to determine when you have reached the correct node.
The reasoning is that the last node will be processed first, then the second last, then the third last and so on

- abhimanipal February 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We could use recursion
In the recursive function call the next node till the end of the list
When the stack is unwinding, use a counter to determine when you have reached the correct node.
The reasoning is that the last node will be processed first, then the second last, then the third last and so on

- abhimanipal February 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can push all the elements to the stack, then pop the stack 5 times in general n times to get the nth element from the tail.

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

takes two pointer p1 and pn where "n" indicate which no. of node from end intially both are intiallized by head node of LL
First move "pn" to n node ahead to p1
if pn == null then return list is less length then n
else
move pn and p1 one step ahead one by one until pn = null
at the end p1 will point nth node from end
ex. n =5 ;
1 -> 2 -> 3 -> 4 -> -> 5 -> 6 -> 7
p1 = 1 and p5 = 5
p5 = 6 so p1 = 2
p5 = 7 so p1 = 3 and p5 = null stop ....and p1 is the 5th node from the end .... :)

test cases
1> list is empty 2> list has less no. of nodes then "n"
3>list is circular 4> list has equal no. of node of "n" 5> list has more than "n" nodes
6> list has only one node 7> list has only one node with circular fashion

- Nitin Gupta iitian June 22, 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