-3

Problem statement:

Consider a library which has millions of books. Thousands of books are being added and removed each minute. The Problem is to find all the books available by using a prefix (given as input) with minimum time and space complexity in java. Example,

Books - {'abcd','abda','aaqe','abc'}

input: ab

output: 3 // because there are 3 book names starting with 'ab'

input: abc

output: 2

input: a

output: 4

This can be done using just loops but as I said before this needs to be done in minimum time and space complexity in java.

Thanks in advance

Update - Since everyone asked me to at least try and write my own code, I am updating this. This problem was given to me in an interview. I tried this using a hashmap and it convinced the interviewer but I'm still thinking that there are some good data structures or algorithms for this purpose. I'm not asking anyone to solve this by writing code for me. I'm just asking an idea on which data structure to use for minimum complexity since I'm still learning data structures and algorithms. Sorry if this is a stupid question. Thank you

Mukesh A
  • 323
  • 1
  • 4
  • 13
Manoj J
  • 23
  • 6
  • 1
    At least share your own attempt... Furthermore take a look at *prefix trees* (also known as *trie*). Then one can search linear in the number of input characters (and linear in the number of matches). – Willem Van Onsem Sep 04 '18 at 08:11
  • It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ] and [ask]. – Federico klez Culloca Sep 04 '18 at 08:11
  • 2
    Relevant: [Comparison of search speed for B-Tree and Trie](https://stackoverflow.com/questions/43309232/comparison-of-search-speed-for-b-tree-and-trie) – Amadan Sep 04 '18 at 08:12

1 Answers1

1

You can use the data structure called Trie to achieve the same.

I found this to be useful as it is concise and explains what a trie is and how it is different from other Data Structures.

You can also refer to other sources online to get a better understanding of Trie.

Mukesh A
  • 323
  • 1
  • 4
  • 13