c - designing a algorithm for a large data -
i read 1 of question being asked job interview of software engineer.
if there 1000 websites , 1000 users, write program , data-structure such can query followin @ real time: 1. given user, list of sites he/she has visited 2. given website, list of users have visited it.
i think wanted sort of pseudo code or designing algorithm..
can guys give tips this?
one thing - in order able answer both queries, need store pairs mean user has visited given website. propose following:
you have structure:
struct visitpair{ int websiteid; int userid; visitpair* nextforuser; visitpair* nextforwebsite; };
nextforuser point next pair given user or null if there no next pair given user, nextforwebsite point next pair website. user , website like:
struct user { char* name; visitpair* firstpair; }; struct website { char* url; visitpair* firstpair; };
i assume both website-s , users stored in arrays, these arrays websites
, users
. adding new visitpair relatively easy:
void addnewpair(int siteid, int userid) { visitpair* newpair = (visitpair*)malloc(sizeof(vizitpair)); newpair->nextforuser = users[userid]->firstpair; users[userid]->firstpair = newpair; newpair->nextforwesite = websites[siteid]->firstpair; websites[siteid]->firstpair = newpair; }
printing users website , websites user done iterating on list should able that.
in short create structure has 2 lists integrated. not think there can solution better complexity 1 has linear complexity respect answer , constant complexity adding pair.
hope helps.
Comments
Post a Comment