Sunteți pe pagina 1din 3

RULES FOR ASSIGNMENT

1. The deadline for sending solutions is 30 November, 2015 12:00 AM, i.e. the midnight of
this Sunday. No solution would be considered for evaluation post the deadline.
2. Students can send their solutions to: iitd.placements.2015@aptportfolio.com. They
should mention their name and entry number clearly in the submission e-mail.
3. They can also post their queries to the same email ID.
4. This should be circulated to people in the short-list as well as the wait-list.
5. Skeleton code has been attached with the mail.
Scenario: An application receives packets from a server on the network. Each packet belongs to
a particular channel, and is also identified by a unique sequence number. Note that each
channel has its own sequence number series. So if the server is sending data for 10 channels,
you should expect to see packets starting with sequence number 1 for each of these 10
channels.
The network is such that after leaving the server but before reaching your application, these
packets could be
1. re-ordered; OR
2. duplicated; BUT
3. never dropped completely.
The goal of your application is to always apply the packet data in-order of (channel-specific)
sequence numbers and without any gaps. We are attaching some skeleton code below. For the
purpose of this evaluation, apply means to just call a function we have provided. Of course in
the real world, there will be more work to be done, but we will ignore it for now.
You have to implement the function re_sequence_and_apply().
You can use any data structure for this purpose, including anything from C++ STL. If you write
your own data structure, please supply the entire definition of that data structure with your
solution.
Additionally, you will have also to implement the event_source interface to test your code the
any way you wish to. That way, you don't have to worry about reading the packets from the
network etc.
Note: Your code should work using any version of your choice of g++ on Linux and you cannot
use any library other than the ones automatically included by g++ on Linux (you can use C++
STL). Otherwise, you are free to use any g++ option. Please provide a Makefile or some other
way to compile your code along with your solution.
Your solution will be evaluated on the basis of correctness, as well as, space and time
complexity. For shortlisted candidates, we might discuss your solution in the interview as well.
#include <stdint.h>
#include <iostream>

struct event {
uint8_t channel_id;
uint32_t sequence_number;
uint8_t *pkt_data;
uint32_t pkt_len;
};

class event_source {
public:
virtual event* get_next() = 0;
virtual void release_event(event* ev) = 0;
};

void apply_pkt_data(
int8_t channel_id, uint32_t sequence_number, uint8_t* pkt_data, uint32_t pkt_len)
{
std::cout <<"(" <<(int)channel_id <<","<<sequence_number
<<","<<*(char*)pkt_data <<") ";
}

void re_sequence_and_apply(
int8_t channel_id, uint32_t sequence_number, uint8_t* pkt_data, uint32_t pkt_len)
{
/* This is the function that you need to code. It should apply the data in sequence
for each
* channel for which data is received. Once your code figures out which packet to
apply
* next, it should call apply_pkt_data() as defined above */
}

int main () {
// Instantiate your own implementation of event_source here for testing.

event_source* event_q;
event* ev;
while ((ev = event_q->get_next()) != NULL) {
re_sequence_and_apply(ev->channel_id, ev->sequence_number, ev->pkt_data,
ev->pkt_len);
event_q->release_event(ev);
}
return 0;
}

// Sample Input and Output


// sample input (X, Y, Z), where X = Channel ID, Y = Sequence Number, Z = Packet
Data
// (1,1,A) (1,2,B) (2,2,C) (2,1,D) (2,1,D) (1,4,F) (2,3,G) (1,1,A) (2,5,H) (2,2,C) (1,3,I)
(1,2,B) (2,4,J) (1,5,K)
//
// sample output as printed by apply_pkt_data
// (1,1,A) (1,2,B) (2,1,D) (2,2,C) (2,3,G) (1,3,I) (1,4,F) (2,4,J) (2,5,H) (1,5,K)

S-ar putea să vă placă și