ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage   interview questions urls   External Links  Contact Us     Login  |  Sign Up                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C++  >>  C++ General
 
 


 

 
 STL interview questions  STL Interview Questions
 OOPS interview questions  OOPS Interview Questions
 C++ General interview questions  C++ General Interview Questions
Question
Memory is not a constraint. In a single iteration(NOTE: you
can't go back), how will you find out the 10th last
node/item in a linked list.
 Question Submitted By :: Ashutosh
I also faced this Question!!     Rank Answer Posted By  
 
  Re: Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find out the 10th last node/item in a linked list.
Answer
# 1
	List *nodes[10]; //asked 10th last, so, only 10 items
	int pos = 0;

	List *ptr = &FirstNode

	while(ptr)
	{
		nodes[(pos%10)] = ptr;
		pos++;
		ptr = ptr->next;
	}
	if(pos>=10)
	{
		printf("Tenth last element is %d",nodes[(pos-10)%10]->data);
	}
	else
	{
		printf("There doesn't exist any 1oth last element");
	}
 
Is This Answer Correct ?    0 Yes 0 No
Ashutosh
 
  Re: Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find out the 10th last node/item in a linked list.
Answer
# 2
Ashutosh,

Can you explain how your answer sync with the question?
 
Is This Answer Correct ?    0 Yes 0 No
Mms Zubeir
 
 
 
  Re: Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find out the 10th last node/item in a linked list.
Answer
# 3
simple,

in case the list has say 90 elements, then for every 10
elements, he is writing to the array. though the array size
is 10, this is done by saying pos%10.
thus when the list is iterated he just substracts 10 from
the pos and then does %10 since he has to factor in, that
array starts from 0 and not from 1 :)
 
Is This Answer Correct ?    0 Yes 0 No
Ravindra
 
  Re: Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find out the 10th last node/item in a linked list.
Answer
# 4
Assume the list is a circular one (implemented by pos%10).
So, when you are iterating, you are going round and round in
a circle. When you reach the end, you just have to go back
exactly 10 items. By going back here means accessing the
element 10 position prior to the current one, which is
implemented by (pos-10)%10

Since, it's a circular array as assumed, you have always
access it by doing pos%10 or (pos-10)%10.

NOTE: The value of pos has always been incremented and never
decremented or set to zero.
 
Is This Answer Correct ?    0 Yes 0 No
Ashutosh
 
  Re: Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find out the 10th last node/item in a linked list.
Answer
# 5
I got it, thank you.

but one suggestion, instead of using

printf("Tenth last element is %d",nodes[(pos-10)%10]->data);

you can simply give,
printf("Tenth last element is %d",nodes[pos%10]->data);
 
Is This Answer Correct ?    0 Yes 0 No
Mms Zubeir
 
  Re: Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find out the 10th last node/item in a linked list.
Answer
# 6
NO, it won't work.

because, after the wile loop, pos is pointing to the last
element NOT 10th last. So, you have to go back 10 elements.
 
Is This Answer Correct ?    0 Yes 0 No
Ashutosh
 
  Re: Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find out the 10th last node/item in a linked list.
Answer
# 7
ListNodePtr* tenthListNodePtr = NULL; //Holds 10th last node

ListNodePtr* tempListNodePtr = firstNode;

int counter = 1;

//Advance the tempListNodePtr to 10th node from first //node.
while( (counter < 10) && (tempListNodePtr) )
{

     tempListNodePtr = tempListNodePtr->nextNode;

     ++counter;
}

tenthListNodePtr  = firstNode;

//Advance both the pointers. also check for cycle. 
// Since two ptrs differ by 10, when last node is reached
//the result will be there.
while( (tempListNodePtr) && (tempListNodePtr != firstNode) )
{

tenthListNodePtr = tenthListNodePtr->nextNode;

tempListNodePtr = tempListNodePtr->nextNode;

}
 
Is This Answer Correct ?    0 Yes 0 No
Vivek
 
  Re: Memory is not a constraint. In a single iteration(NOTE: you can't go back), how will you find out the 10th last node/item in a linked list.
Answer
# 8
ahutosh ,the kid is right pos% 10 will do fine,,are u
nuts,work on basic maths
 
Is This Answer Correct ?    0 Yes 0 No
Dg
 

 
 
 
Other C++ General Interview Questions
 
  Question Asked @ Answers
 
implement stack using stack.h headerfile functions Subex1
In C++ cout is: a) object b) class c) something else Lehman-Brothers10
When the design recommends static functions? Symphony1
When volatile can be used? Symphony2
What is the Difference between "printf" and "sprintf"? iSoft2
what is importance of data sturture in a programming language? L&T10
What will happen if I allocate memory using "new" and free it using "free" or allocate sing "calloc" and free it using "delete"?  2
Shall we use 'free' to free memory assigned by new, What are the further consequences?? Symphony4
What is the Difference between "vector" and "array"? TCS6
What is "map" in STL?  1
How can you find the nodes with repetetive data in a linked list? Lucent1
In a class, there is a reference or pointer of an object of another class embedded, and the memory is either allocated or assigned to the new object created for this class. In the constructor, parameters are passed to initialize the data members and the embedded object reference to get inialized. What measures or design change should be advised for proper destruction and avioding memory leaks, getting pointers dangling for the embedded object memory allocation? Please suggest. GE3
What is Name Decoration? Lucent2
template<class T, class X> class Obj { T my_t; X my_x; public: Obj(T t, X x) : my_t(t), my_x(x) { } }; Referring to the sample code above, which one of the following is a valid conversion operator for the type T? a) T operator T () { return my_t; } b) T operator(T) const { return my_t; } c) operator(T) { return my_t; } d) T operator T (const Obj &obj) { return obj.my_t; } e) operator T () const { return my_t; } Quark1
What is difference between initialization and assignment? HP4
The "virtual" specifier in a member function enables which one of the following? a) Monmorphism b) Late binding c) Metamorphism d) Solomorphism e) Inheritance Quark3
What are Binay tress and B trees? Diff between them? CTS1
What is the difference between public, private, protected inheritance? Wipro4
Explain the need for "Virtual Destructor"? Infosys1
this is to swap to strings....but in output the whole strings are swapped leaving first as it is...why it is so #include<iostream.h> int main() { char a[]="ajeet"; char b[]="singh"; long x=*a; long y=*b; cout<<x<<":"<<y; x=x+y; y=x-y; x=x-y; *a=x; *b=y; cout<<x<<":"<<y; cout<<&a<<endl; cout<<&b<<endl; }  1
 
For more C++ General Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com