473,231 Members | 2,494 Online

function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

And please dont think this is my homework.
Can anybody simple tell me how this can be done. I am preparing for an
interview and wanted to know how to go about implementing this.
Or atleast tell me where I can find few examples on traversing Lists.
Even if I can get any good resources which explains lists properly my
job will be done.
Jul 22 '05 #1
32 5648
"source" <su*********@hotmail.com> wrote in message
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

Show us your best effort so far. Also try to write a function that finds
the 5th element in a container matching a certain condition (for example,
the element is >10).

Jul 22 '05 #2
"source" <su*********@hotmail.com> wrote in message
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

Show us your best effort so far. Also try to write a function that finds
the 5th element in a container matching a certain condition (for example,
the element is >10).

Jul 22 '05 #3
source writes:
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

And please dont think this is my homework.
Can anybody simple tell me how this can be done. I am preparing for an
interview and wanted to know how to go about implementing this.
Or atleast tell me where I can find few examples on traversing Lists.
Even if I can get any good resources which explains lists properly my
job will be done.

If I understand the question, it can't be done; at least on a bare bones
linked list. You will have to count the elements so you can subtract five
from that. And then traverse the list again, which would be easiest from
the head end. I call that two passes, not one that you asked for. My
default meaning for end is the far end; I call the other end the beginning.

For test cases, test for within range, each valid boundary and one beyond
each valid boundary. Valid because numbering might start with zero or one.
Jul 22 '05 #4
source writes:
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

And please dont think this is my homework.
Can anybody simple tell me how this can be done. I am preparing for an
interview and wanted to know how to go about implementing this.
Or atleast tell me where I can find few examples on traversing Lists.
Even if I can get any good resources which explains lists properly my
job will be done.

If I understand the question, it can't be done; at least on a bare bones
linked list. You will have to count the elements so you can subtract five
from that. And then traverse the list again, which would be easiest from
the head end. I call that two passes, not one that you asked for. My
default meaning for end is the far end; I call the other end the beginning.

For test cases, test for within range, each valid boundary and one beyond
each valid boundary. Valid because numbering might start with zero or one.
Jul 22 '05 #5
"osmium" <r1********@comcast.net> wrote in message news:c4pnvn\$2kbbqu\$1@ID-
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases against that function.
If I understand the question, it can't be done; at least on a bare bones
linked list. You will have to count the elements so you can subtract five
from that. And then traverse the list again, which would be easiest from
the head end. I call that two passes, not one that you asked for. My
default meaning for end is the far end; I call the other end the beginning.

You can do it in one pass. The first way to implement it could actually be
slower than the 2 pass method, and the second way would probably be faster.

For test cases, test for within range, each valid boundary and one beyond

one.

I'd say the empty list, list of 1 element, list of 5 elements, list of >5
elements.
Jul 22 '05 #6
"osmium" <r1********@comcast.net> wrote in message news:c4pnvn\$2kbbqu\$1@ID-
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases against that function.
If I understand the question, it can't be done; at least on a bare bones
linked list. You will have to count the elements so you can subtract five
from that. And then traverse the list again, which would be easiest from
the head end. I call that two passes, not one that you asked for. My
default meaning for end is the far end; I call the other end the beginning.

You can do it in one pass. The first way to implement it could actually be
slower than the 2 pass method, and the second way would probably be faster.

For test cases, test for within range, each valid boundary and one beyond

one.

I'd say the empty list, list of 1 element, list of 5 elements, list of >5
elements.
Jul 22 '05 #7
On 4 Apr 2004 12:05:51 -0700 in comp.lang.c++, su*********@hotmail.com
(source) wrote,
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

This sentence no verb.

What I consider the usual approach to that kind of thing is what I call
a "following pointer". That is, you have two iterators, one points to
where you are looking in the list and the other five items behind it.
You advance both of them at the same time. When you get to what you
were looking for (in this case, the end) the "following" iterator points
to what you wanted.

No doubt you can write the C++ code based on that.

Jul 22 '05 #8
On 4 Apr 2004 12:05:51 -0700 in comp.lang.c++, su*********@hotmail.com
(source) wrote,
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

This sentence no verb.

What I consider the usual approach to that kind of thing is what I call
a "following pointer". That is, you have two iterators, one points to
where you are looking in the list and the other five items behind it.
You advance both of them at the same time. When you get to what you
were looking for (in this case, the end) the "following" iterator points
to what you wanted.

No doubt you can write the C++ code based on that.

Jul 22 '05 #9
source wrote:
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

And please dont think this is my homework.
Can anybody simple tell me how this can be done. I am preparing for an
interview and wanted to know how to go about implementing this.
Or atleast tell me where I can find few examples on traversing Lists.
Even if I can get any good resources which explains lists properly my
job will be done.

P-code, and without error checking (assumes list is at least 5 long):

node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

for(;;)
{
if(node5->next == 0)
{
return node->val;
}
node = node->next;
node5 = node5->next;
}

- Pete
Jul 22 '05 #10
source wrote:
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

And please dont think this is my homework.
Can anybody simple tell me how this can be done. I am preparing for an
interview and wanted to know how to go about implementing this.
Or atleast tell me where I can find few examples on traversing Lists.
Even if I can get any good resources which explains lists properly my
job will be done.

P-code, and without error checking (assumes list is at least 5 long):

node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

for(;;)
{
if(node5->next == 0)
{
return node->val;
}
node = node->next;
node5 = node5->next;
}

- Pete
Jul 22 '05 #11
osmium wrote:
source writes:

function that would: return the 5th element from the end in a singly
linked list of integers, in one pass,

If I understand the question, it can't be done; at least on a bare bones
linked list. You will have to count the elements so you can subtract five
from that. And then traverse the list again, which would be easiest from
the head end. I call that two passes, not one that you asked for. My
default meaning for end is the far end; I call the other end the beginning.

Keep two pointers. Begin traversing the list with the first. 5 elements
in, begin traversing with the second. Once the first reaches the end,
the second will be 5 elements from the end.

-Kevin
--
My email address is valid, but changes periodically.
Jul 22 '05 #12
osmium wrote:
source writes:

function that would: return the 5th element from the end in a singly
linked list of integers, in one pass,

If I understand the question, it can't be done; at least on a bare bones
linked list. You will have to count the elements so you can subtract five
from that. And then traverse the list again, which would be easiest from
the head end. I call that two passes, not one that you asked for. My
default meaning for end is the far end; I call the other end the beginning.

Keep two pointers. Begin traversing the list with the first. 5 elements
in, begin traversing with the second. Once the first reaches the end,
the second will be 5 elements from the end.

-Kevin
--
My email address is valid, but changes periodically.
Jul 22 '05 #13
"Pete" <x@x.x> wrote in message news:yKZbc.14870
node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

What happens on a list of 0 to 4 elements?

Jul 22 '05 #14
"Pete" <x@x.x> wrote in message news:yKZbc.14870
node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

What happens on a list of 0 to 4 elements?

Jul 22 '05 #15
Siemel Naran wrote:
"Pete" <x@x.x> wrote in message news:yKZbc.14870

node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

What happens on a list of 0 to 4 elements?

As he noted:

"P-code, and without error checking (assumes list is at least 5 long):"

mark

Jul 22 '05 #16
Siemel Naran wrote:
"Pete" <x@x.x> wrote in message news:yKZbc.14870

node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

What happens on a list of 0 to 4 elements?

As he noted:

"P-code, and without error checking (assumes list is at least 5 long):"

mark

Jul 22 '05 #17
"Pete" <x@x.x> wrote in message news:yKZbc.14870
node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

for(;;)
{
if(node5->next == 0)
{
return node->val;
}
node = node->next;
node5 = node5->next;
}

Is it really faster than the 2 pass method? In the 2 pass method, first
visit all N elements for a total of N calls to const_iterator::operator++.
Then you perform N-5 more calls to operator++. But in the 1 pass method,
you perform N calls to const_iterator::operator++ on node, and N-5 on node5.
So don't they both have the same running time?
Jul 22 '05 #18
"Pete" <x@x.x> wrote in message news:yKZbc.14870
node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

for(;;)
{
if(node5->next == 0)
{
return node->val;
}
node = node->next;
node5 = node5->next;
}

Is it really faster than the 2 pass method? In the 2 pass method, first
visit all N elements for a total of N calls to const_iterator::operator++.
Then you perform N-5 more calls to operator++. But in the 1 pass method,
you perform N calls to const_iterator::operator++ on node, and N-5 on node5.
So don't they both have the same running time?
Jul 22 '05 #19
Siemel Naran wrote:
"Pete" <x@x.x> wrote in message news:yKZbc.14870
node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

for(;;)
{
if(node5->next == 0)
{
return node->val;
}
node = node->next;
node5 = node5->next;
}

Is it really faster than the 2 pass method? In the 2 pass method,
first visit all N elements for a total of N calls to
const_iterator::operator++. Then you perform N-5 more calls to
operator++. But in the 1 pass method, you perform N calls to
const_iterator::operator++ on node, and N-5 on node5. So don't they
both have the same running time?

No, it really wouldn't be faster much if at all, but it's much more
"elegant" IMHO.

- Pete
Jul 22 '05 #20
Siemel Naran wrote:
"Pete" <x@x.x> wrote in message news:yKZbc.14870
node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

for(;;)
{
if(node5->next == 0)
{
return node->val;
}
node = node->next;
node5 = node5->next;
}

Is it really faster than the 2 pass method? In the 2 pass method,
first visit all N elements for a total of N calls to
const_iterator::operator++. Then you perform N-5 more calls to
operator++. But in the 1 pass method, you perform N calls to
const_iterator::operator++ on node, and N-5 on node5. So don't they
both have the same running time?

No, it really wouldn't be faster much if at all, but it's much more
"elegant" IMHO.

- Pete
Jul 22 '05 #21
On Mon, 05 Apr 2004 00:42:42 GMT in comp.lang.c++, "Siemel Naran"
<Si*********@REMOVE.att.net> wrote,
Is it really faster than the 2 pass method? In the 2 pass method, first
visit all N elements for a total of N calls to const_iterator::operator++.
Then you perform N-5 more calls to operator++. But in the 1 pass method,
you perform N calls to const_iterator::operator++ on node, and N-5 on node5.
So don't they both have the same running time?

Nobody said it was faster. The problem spec said "in one pass".
The question is, with both pointers running through the list like that
does it count as one pass, or two passes running not quite in parallel?

Jul 22 '05 #22
On Mon, 05 Apr 2004 00:42:42 GMT in comp.lang.c++, "Siemel Naran"
<Si*********@REMOVE.att.net> wrote,
Is it really faster than the 2 pass method? In the 2 pass method, first
visit all N elements for a total of N calls to const_iterator::operator++.
Then you perform N-5 more calls to operator++. But in the 1 pass method,
you perform N calls to const_iterator::operator++ on node, and N-5 on node5.
So don't they both have the same running time?

Nobody said it was faster. The problem spec said "in one pass".
The question is, with both pointers running through the list like that
does it count as one pass, or two passes running not quite in parallel?

Jul 22 '05 #23
Siemel Naran wrote:
"Pete" <x@x.x> wrote in message news:yKZbc.14870
node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

for(;;)
{
if(node5->next == 0)
{
return node->val;
}
node = node->next;
node5 = node5->next;
}

Is it really faster than the 2 pass method? In the 2 pass method,
first visit all N elements for a total of N calls to
const_iterator::operator++. Then you perform N-5 more calls to
operator++. But in the 1 pass method, you perform N calls to
const_iterator::operator++ on node, and N-5 on node5. So don't they
both have the same running time?

BTW, this exercise is a good example of a good reason to use a doubly linked
list, because it's easy to go either forwards or back depending on the
situation. The extra memory used for the extra pointer per node really isn't
an issue, usually.

- Pete
Jul 22 '05 #24
Siemel Naran wrote:
"Pete" <x@x.x> wrote in message news:yKZbc.14870
node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

for(;;)
{
if(node5->next == 0)
{
return node->val;
}
node = node->next;
node5 = node5->next;
}

Is it really faster than the 2 pass method? In the 2 pass method,
first visit all N elements for a total of N calls to
const_iterator::operator++. Then you perform N-5 more calls to
operator++. But in the 1 pass method, you perform N calls to
const_iterator::operator++ on node, and N-5 on node5. So don't they
both have the same running time?

BTW, this exercise is a good example of a good reason to use a doubly linked
list, because it's easy to go either forwards or back depending on the
situation. The extra memory used for the extra pointer per node really isn't
an issue, usually.

- Pete
Jul 22 '05 #25
* su*********@hotmail.com (source) schriebt:
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

What is your definition of "one pass", and does this need to be
non-destructive?

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #26
* su*********@hotmail.com (source) schriebt:
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

What is your definition of "one pass", and does this need to be
non-destructive?

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #27
"Pete" <x@x.x> wrote in message news:yF2cc.15284
Siemel Naran wrote: node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

for(;;)
{
if(node5->next == 0)
{
return node->val;
}
node = node->next;
node5 = node5->next;
}
Is it really faster than the 2 pass method? In the 2 pass method,
first visit all N elements for a total of N calls to
const_iterator::operator++. Then you perform N-5 more calls to
operator++. But in the 1 pass method, you perform N calls to
const_iterator::operator++ on node, and N-5 on node5. So don't they
both have the same running time?

Seems the 2 pass method you maintain an index for looping from [0, N-5) but
in the 1 pass method you don't. So based on that the 1 pass method might be
faster, though the improvement is probably negligible.

BTW, this exercise is a good example of a good reason to use a doubly linked list, because it's easy to go either forwards or back depending on the
situation. The extra memory used for the extra pointer per node really isn't an issue, usually.

If going back is not a common operation, the single list could be better.
Anyway, the point of these 'clever' interview questions is not always to
mimic reality, but to see how you think through problems, especially the
logic questions.

Anyway, I think if you just store the value of the pointer in one pass, you
could get better
node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

T * last[5] = { &node->val, &node->next->val, ... };

for(int i=0; ; i = (i==5 ? 0 : 5))
{
if(node5->next == 0)
{
return last[i];
}
last[i] = &node5->val;
node5 = node5->next;
}
Jul 22 '05 #28
"Pete" <x@x.x> wrote in message news:yF2cc.15284
Siemel Naran wrote: node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

for(;;)
{
if(node5->next == 0)
{
return node->val;
}
node = node->next;
node5 = node5->next;
}
Is it really faster than the 2 pass method? In the 2 pass method,
first visit all N elements for a total of N calls to
const_iterator::operator++. Then you perform N-5 more calls to
operator++. But in the 1 pass method, you perform N calls to
const_iterator::operator++ on node, and N-5 on node5. So don't they
both have the same running time?

Seems the 2 pass method you maintain an index for looping from [0, N-5) but
in the 1 pass method you don't. So based on that the 1 pass method might be
faster, though the improvement is probably negligible.

BTW, this exercise is a good example of a good reason to use a doubly linked list, because it's easy to go either forwards or back depending on the
situation. The extra memory used for the extra pointer per node really isn't an issue, usually.

If going back is not a common operation, the single list could be better.
Anyway, the point of these 'clever' interview questions is not always to
mimic reality, but to see how you think through problems, especially the
logic questions.

Anyway, I think if you just store the value of the pointer in one pass, you
could get better
node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

T * last[5] = { &node->val, &node->next->val, ... };

for(int i=0; ; i = (i==5 ? 0 : 5))
{
if(node5->next == 0)
{
return last[i];
}
last[i] = &node5->val;
node5 = node5->next;
}
Jul 22 '05 #29
> Is it really faster than the 2 pass method?

If the time for retrieval of one element is high, then a one pass approach
is faster than a two pass.
However the method of using two pointers are actually a two pass algorithm
in that case (each element is loaded/passed twice).
A one pass algorithm, that would save time, would cache the elements once
loaded and maintain a local 5 element list.

Niels Dybdahl
Jul 22 '05 #30
> Is it really faster than the 2 pass method?

If the time for retrieval of one element is high, then a one pass approach
is faster than a two pass.
However the method of using two pointers are actually a two pass algorithm
in that case (each element is loaded/passed twice).
A one pass algorithm, that would save time, would cache the elements once
loaded and maintain a local 5 element list.

Niels Dybdahl
Jul 22 '05 #31
osmium wrote:

source writes:
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

And please dont think this is my homework.
Can anybody simple tell me how this can be done. I am preparing for an
interview and wanted to know how to go about implementing this.
Or atleast tell me where I can find few examples on traversing Lists.
Even if I can get any good resources which explains lists properly my
job will be done.

If I understand the question, it can't be done; at least on a bare bones
linked list. You will have to count the elements so you can subtract five
from that. And then traverse the list again, which would be easiest from
the head end. I call that two passes, not one that you asked for. My
default meaning for end is the far end; I call the other end the beginning.

Or simply:
Use a queue to store exactly 5 pointers to nodes as you traverse the list.
When you have reached the end of the list, the queue will contain pointers
to the last 5 list elements.
--
Karl Heinz Buchegger
Jul 22 '05 #32
osmium wrote:

source writes:
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

And please dont think this is my homework.
Can anybody simple tell me how this can be done. I am preparing for an
interview and wanted to know how to go about implementing this.
Or atleast tell me where I can find few examples on traversing Lists.
Even if I can get any good resources which explains lists properly my
job will be done.

If I understand the question, it can't be done; at least on a bare bones
linked list. You will have to count the elements so you can subtract five
from that. And then traverse the list again, which would be easiest from
the head end. I call that two passes, not one that you asked for. My
default meaning for end is the far end; I call the other end the beginning.

Or simply:
Use a queue to store exactly 5 pointers to nodes as you traverse the list.
When you have reached the end of the list, the queue will contain pointers
to the last 5 list elements.
--
Karl Heinz Buchegger
Jul 22 '05 #33

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