If you’re interviewing for a backend position dealing with lots of data, your interview might ask you this question.
Source: I’ve been asked this question before in the distant past
My (wrong) past answer + train of thought
In SQL, we do lots of SELECT * FROM table_name WHERE id = SOME_ID
The fastest way to access one value would be to use a dictionary (hashmap)
Me: A SQL table probably uses a dictionary data structure as it takes O(1) time to access a certain entry by id.
My interviewer then made a face
He wasn’t too impressed.
Interviewer: But what if I want to find many entries from indexes say 50–100? Would a dictionary still be the best choice?
Me: (oops dictionary isn’t the right answer)
Me: Hm maybe it’s a list (proceeds to spout some gibberish)
The Answer
B-tree.
Let’s say our table stores 400 dog entries.
There are 2 types of nodes:
- index nodes that store the start and end indexes (top row)