473,762 Members | 8,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete every mth element in a linked list ??

This is a normal interview question, which goes like this
"Given a linked list of integers, delete every mth node and return the
last remaining node."

eg: Linked list = 4->6->7->3->5->6->10->1->23->17
Delete every 3rd node (m = 3) and return the last remaining node
meaning...
4->6->3->5->10->1->17
4->3->5->1->17
3->5->17
3->17
3

after deleting every 3rd node the last remaining node is 3, so node
with data 3 is returned.
function prototype:

node* deleteEveryMth( node** head, int m)
{
// .... your code

}

thanks
A

Dec 20 '06 #1
10 4320
Aditya said:
This is a normal interview question, which goes like this
"Given a linked list of integers, delete every mth node and return the
last remaining node."

eg: Linked list = 4->6->7->3->5->6->10->1->23->17
Delete every 3rd node (m = 3) and return the last remaining node
meaning...
4->6->3->5->10->1->17
4->3->5->1->17
3->5->17
3->17
3

after deleting every 3rd node the last remaining node is 3, so node
with data 3 is returned.
function prototype:

node* deleteEveryMth( node** head, int m)
{
// .... your code
No, yours.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 20 '06 #2
Aditya wrote:
This is a normal interview question, which goes like this
"Given a linked list of integers, delete every mth node and return the
last remaining node."
Please don't post to multiple groups, cross post if you must, or better
still, post to one group.

--
Ian Collins.
Dec 20 '06 #3
On 20 Dec 2006 01:36:00 -0800, "Aditya" <ad*****@gmail. comwrote:
>This is a normal interview question, which goes like this
I've never asked this question in an interview, nor do I ever plan to.
I guess I must be abnormal.
>"Given a linked list of integers, delete every mth node and return the
last remaining node."

eg: Linked list = 4->6->7->3->5->6->10->1->23->17
Delete every 3rd node (m = 3) and return the last remaining node
meaning...
4->6->3->5->10->1->17
4->3->5->1->17
3->5->17
3->17
3

after deleting every 3rd node the last remaining node is 3, so node
with data 3 is returned.
function prototype:

node* deleteEveryMth( node** head, int m)
{
// .... your code

}
I have no clue.

But if, and when, I ever need to know how to delete every mth node in
a linked list of integers, I'll spend the time to learn how to do it
right, and I'll get it right. And the customer will pay me dearly for
it.

At an interview, I start off with asking very simple questions, like
what's wrong with the following statements?

void main(void);
i = i++ % MAX_VAL;
p = (int*)malloc(4 * 4);
int main(void) {return -1;}
goto ERROR;
gets(s);
fflush(STDIN);
#define N X+1
unsigned i; for(i=1;i>=0;i--)
while(1)

Once they answer those questions, it's usually easy to determine where
the interview is headed.

--
jay
Dec 20 '06 #4
jaysome said:

<snip>
>
At an interview, I start off with asking very simple questions,
Ooh, ooh, I love these...
like
what's wrong with the following statements?

void main(void);
It's not a statement.
i = i++ % MAX_VAL;
MAX_VAL is undefined, so you can't take its percentage.
p = (int*)malloc(4 * 4);
(int*): malloc returns a pointer, and you can't multiply ints by pointers.
int main(void) {return -1;}
The actual program has been left out.
goto ERROR;
goto can't be an error; it's a keyword, so it's built into the language.
gets(s);
Should be: s = gets(s);
fflush(STDIN);
Ah, good old case sensitivity. This should of course be FFLUSH(STDIN);
#define N X+1
As everyone knows, X stands for unknown, so X+1 is meaningless.
unsigned i; for(i=1;i>=0;i--)
i is a lousy name for a variable. Better:

unsigned eternity_ring; for(eternity_ri ng = 1;
eternity_ring >= 0;
eternity_ring --)

Note the cool formatting, which should ensure that I get 11/10 for this
quiz.
while(1)
Should be while(2), as it is the second loop in this test.
Once they answer those questions, it's usually easy to determine where
the interview is headed.
How did I do? :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 20 '06 #5

"Aditya" <ad*****@gmail. comwrote in message
news:11******** **************@ a3g2000cwd.goog legroups.com...
This is a normal interview question, which goes like this
"Given a linked list of integers, delete every mth node and return the
last remaining node."

eg: Linked list = 4->6->7->3->5->6->10->1->23->17
Delete every 3rd node (m = 3) and return the last remaining node
meaning...
4->6->3->5->10->1->17
4->3->5->1->17
3->5->17
3->17
3

after deleting every 3rd node the last remaining node is 3, so node
with data 3 is returned.
function prototype:

node* deleteEveryMth( node** head, int m)
{
// .... your code

}

thanks
A
The example looks broken. And how do you delete the 3rd node
from a list with two entries?
Dec 20 '06 #6

"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:C_******** *************** *******@bt.com. ..
jaysome said:

<snip>

At an interview, I start off with asking very simple questions,

Ooh, ooh, I love these...
unsigned i; for(i=1;i>=0;i--)

i is a lousy name for a variable. Better:

unsigned eternity_ring; for(eternity_ri ng = 1;
eternity_ring >= 0;
eternity_ring --)

Note the cool formatting, which should ensure that I get 11/10 for this
quiz.
while(1)

Should be while(2), as it is the second loop in this test.
I would have missed this one I would have said it should be:
unsigned i; for(i=1;i>=0;i--)
Dec 20 '06 #7
Barry said:

<snip>
The example looks broken. And how do you delete the 3rd node
from a list with two entries?
Like this.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 20 '06 #8
2006-12-20 <uf************ *************** *****@4ax.com>,
jaysome wrote:
void main(void);
Nothing, assuming that main is never used in the scope in which this
declaration appears.
p = (int*)malloc(4 * 4);
There's several things wrong with it. The most obvious one is casting
malloc, but there's also the assumption that int has a size of 4.
int main(void) {return -1;}
The range of values that main may return is implementation-defined. On
some implementations (this answer is appropriate if interviewing for
a position where you know you'll be programming for windows or unix), it
must be between 0 and 255. For portable code, it must be 0,
EXIT_SUCCESS, or EXIT_FAILURE.
goto ERROR;
ERROR is a reserved identifier. (I hope this is what you were looking
for, rather than some trite condemnation of the use of "goto" without
seeing the context)
fflush(STDIN);
Nothing, assuming that STDIN is of type FILE * and points to a file
opened for output. However, isn't that a bit of a trick question? It'd
be bad programming practice to use that name anyway I guess.
unsigned i; for(i=1;i>=0;i--)
i will wrap around to at least 65535 after reaching 0; it will never
become less than 0.
while(1)
Not sure I see the problem here. for(;;) might be better style.

If you think it's less efficient, this says more about the interviewer
than the interviewee.
Once they answer those questions, it's usually easy to determine where
the interview is headed.
Dec 20 '06 #9
On Wed, 20 Dec 2006 10:42:51 +0000, Richard Heathfield
<rj*@see.sig.in validwrote:
>jaysome said:

<snip>
>>
At an interview, I start off with asking very simple questions,

Ooh, ooh, I love these...
>like
what's wrong with the following statements?

void main(void);

It's not a statement.
>i = i++ % MAX_VAL;

MAX_VAL is undefined, so you can't take its percentage.
>p = (int*)malloc(4 * 4);

(int*): malloc returns a pointer, and you can't multiply ints by pointers.
>int main(void) {return -1;}

The actual program has been left out.
>goto ERROR;

goto can't be an error; it's a keyword, so it's built into the language.
>gets(s);

Should be: s = gets(s);
>fflush(STDIN );

Ah, good old case sensitivity. This should of course be FFLUSH(STDIN);
>#define N X+1

As everyone knows, X stands for unknown, so X+1 is meaningless.
>unsigned i; for(i=1;i>=0;i--)

i is a lousy name for a variable. Better:

unsigned eternity_ring; for(eternity_ri ng = 1;
eternity_ring >= 0;
eternity_ring --)

Note the cool formatting, which should ensure that I get 11/10 for this
quiz.
>while(1)

Should be while(2), as it is the second loop in this test.
>Once they answer those questions, it's usually easy to determine where
the interview is headed.

How did I do? :-)
Further interrogation would be required to determine that. I must
admit that, based on your answers, I would need to excuse myself for a
moment while I grabbed my Schildt hard-copy of the standard. I'd
recite a good number of carefully chosen passages from the text of the
book, and simply ask you after each citation: "Am I reading from the
left or the right page"? Answer them all and you'd score an 11.

Happy Holidays
--
jay

Dec 21 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2420
by: Timothy Madden | last post by:
Hello I have a linked list of object of a class. I thought it would be nice to have the destructor delete the whole list when I delete just the first element. I don't want to recursivly destroy the list elements; the list is arbitrary long. Something like this: class ListElem { char Datum;
3
7661
by: arvind.nm | last post by:
hi, if i create a linked list like list<char> stk; if i want to remove the first element from the list and i write stk.pop_front() the node is still present but the char stored is now '\0',why? how can i delete the first node. will stk.erase(stk.begin()) work?
2
5959
by: PRadyut | last post by:
in this code the delete function does not delete the last node of the double linked list the code ---------------------------------------------------------------- #include <stdio.h> #include <stdlib.h> #include <conio.h>
4
9830
by: eksamor | last post by:
I have a simple linked list: struct element { struct element *next; int start; }; struct list { struct element *head;
5
535
by: Aditya | last post by:
This is a normal interview question, which goes like this "Given a linked list of integers, delete every mth node and return the last remaining node." eg: Linked list = 4->6->7->3->5->6->10->1->23->17 Delete every 3rd node (m = 3) and return the last remaining node meaning... 4->6->3->5->10->1->17 4->3->5->1->17 3->5->17
10
1115
by: ac.c.2k7 | last post by:
Hello Everyone, The solution to this is to copy the data from the next node into this node and delete the next node!. 1. But if the node to be deleted is the last node. Then what should we do ? 2. If the list is Head node? 3 If the list is circular then what all conditions we need to check? Thanks,
11
5637
by: hotice | last post by:
How to write code to delete a specific node in a single link list that takes O(1) time? £¨ link list uses pointers, not hash. £© That is, the time deleting a node is the same (independent from the length of the list. Show your c/c++ source code.
7
5918
by: =?utf-8?B?5YiY5piK?= | last post by:
Hi, folks, Is it possible to delete an element from a sorted array with O(1) time? Best regards
0
10137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9927
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9812
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3914
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.