Entrevista- Para encontrar a la persona con el máximo de boletos de lotería

Given a set of records of the form (ticket no, person name), ticket nos. are unique. find the person having the maximum number of tickets.

I think Hashmap could be an approach for this question but here the ticket no. are unique.So, how can we get the person with maximum number of lottery tickets? Wouldn't we need another structure which will hold the information concerning each person.In initial problem we cannot have person name as the key of hashmap as names cannot be unique.

¿Alguna idea de cómo podemos hacer esto?

preguntado el 12 de febrero de 14 a las 07:02

I did not reallyget your question. why dont you just run a loop and scan through each element and see which one has the most ticets -

@DakshShah that is a very naive approach.what the interviewer here is possibly looking is that is there a way better than that -

1 Respuestas

Assuming you have some way of uniquely identifying a person (person name would have to be unique), you can map person name to a collection of tickets. Then you just need to iterate through that map to figure out which collection is the largest.

If this is just a one time calculation, you can just use a map of person's name to current count of tickets, eliminating the need of the collection.

Note that if person name is not unique, there's no way of telling whether "J Smith" with ticket 1 is the same person as "J Smith" with ticket 2.

Respondido 12 Feb 14, 07:02

that is what I also said in the post that name cannot be a key as it cannot be unique.What I wanted to know is that is there any better approach than the basic iteration one.For once I thought hashmap could be but I couldn't reach to a solution using that - user2916886

I'm not sure how this is possible without a way to uniquely identify a person, either by name or some other property. If all people have the same name (for example), it's just a string property, and there's no way to tell if "A" with ticket 1 and "A" with ticket 2 are the same person or different people. See edit to make this issue more clear. - Krease

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.