Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort out Books - Search related question

I've recently been asked a question in technical interview that goes like this.

You have to find a specific book in a library, and the moment you went inside, you saw that all the books are scattered all around the library, i.e. books are not placed in organized way inside library. How will you proceed to look for that specific book? There is no specification about the book except that you know the Name of the Book.

I know this is related to some search algorithm, but what algorithm can be used for searching book in this case?

like image 219
Vicky Avatar asked Nov 05 '22 23:11

Vicky


1 Answers

Perform a linear search starting at the first book and searching each book until you come across the first occurrence of the the book you want (there may be multiple copies of course).

If you are the sole searcher and there are many books, then sorting them before looking for the book would seem like a hugely inefficient waste of time - unless you intend to search for more books in the future.

You could always ask the librarian to tell you where the book is on their system or you could enlist some friends to help you search and split the problem up and work in parallel.

EDIT There is also a quantum algorithm called Grovers algorithm (if you're in to that sort of thing) that is faster than a linear search for an unsorted database but I don't know too much about it to be honest.

like image 94
Peter Kelly Avatar answered Nov 15 '22 07:11

Peter Kelly