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