typedef struct node * pnode;in C#
struct node {
int value;
node * next;
};
pnode list;
public sealed class nodeYou should implement a function GetElementFromEnd(pnode list,int index) (there's no pnode parameter in the C# version) which returns the value from the specified node, as fast as possible. The C++ version can use this C version or feel free to write it to use a class. On a 3 year old PC, the C version runs in under two seconds. The GetElementFromEndNode() as implemented only fetches the value at the last node.
{
private static Random Rnd = new Random() ;
public int value=Rnd.Next(32000) ;
public node next=null;
}
The list is be populated with 10,000 random values (in the range 1 to 32,000) and your function will be called to fetch 10,000 of them in random order. To make it more challenging, you must NOT use any kind of index array or make any changes to the node struct or class or use a derived class.
To simplify this problem, I've provided source code examples for both C/C++ and C# which includes timing code. Just download the source code examples, flesh out the GetElementFromEnd(pnode list, int index ) function and submit your entire source code file. That's it!
- Download fastlist.zip C/C++ Version
- Download fastlistcs.zip C# Version
Results
There were seven entries, one of which didn't run - it hit an exception and two of which used index tables. Strangely enough those two got the fastest times and one used a construction I have never ever seen used- it's worth repeating below! Don't mock, though it was the fastest. However the challenge specifically excluded index tables.#define P ptr=ptr->next->next->next->next->next->next->next->next->next->next-> next->next->next->next->next->next->next->next->next->next->next-> next->next->next->next->next->next->next->next->next->next->next-> next->next->next->next->next->next->next->next->next->next->next-> next->next->next->next->next->next->next->next->next->next->next-> next->next->next->next->next->next->next->next->next->next->next-> next->next->next->next->next->next->next->next->next->next->next-> next->next->next->next->next->next->next->next->next->next->next-> next->next->next->next->next->next->next->next->next->next->next-> next->nextThe fastest non-indexed are listed below:
- 0.393 seconds - N.Navaneetha Krishnan (India) in C#
- 0.402 seconds - Rob Cartaino (USA) in C#
- 0.746 seconds - Mohammad Motieian Najar (Canada) in C
- 1.31 seconds - Paul Dallas (UK) in C
- 0.00602 seconds - Sattar Sattari (Iran) in C
- 0.156 seconds - Chris Greth (USA) in C++
More Programming Challenges
- Link to Rock, Scissors and Paper Bot Ongoing Programming Challenge - Runs weekly until 2008.
- Link to Programming Challenge 1 - Completed
- Link to Programming Challenge 2 - Completed
- Link to Programming Challenge 3 - Completed
- Link to Programming Challenge 4 - Completed
- Link to Programming Challenge 5 - Completed
- Link to Programming Challenge 7 - Validate chess Positions- Deadline December 31st
- Link to Programming Challenge 8 - Score Texas Holdem Poker Hands - (Deadline January 31 2008)

