Search algorithms Flashcards

1
Q

Time complexity of linear search

A

O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Time complexity of hash table search

A

O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Time complexity of binary search

A

O(logn)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

For a hash table, a perfect hash function should:

A

Takes an input key of any length

Returns an output hash index of fixed size

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

For a hash table, a hash index should be:

A

Unique to the key

Same for equal keys

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

For a hash table, a key should be:

A

Immutable (so that its hashed index is always the same)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Name 2 collision resolution methods for hashtable

A

Separate chaining, linear probing (open addressing)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is hashing?

A

Hashing is the process of calculating the address (index) of a piece of data from its key using a hash function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The key steps for Binary Search are:

A
  1. Handle the base case (empty/single-element array)
  2. Initialise l = 0 and r = array.length-1
  3. Determine middle index mid
  4. Check element at mid: if match, return mid; if less than key, update r to mid-1; if greater than key, update l to mid+1
  5. If l==r (and no match found), return none or raise error, else repeat from step 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The key steps for Linear Search are:

A
  1. Iterate through the array and compare the element in the array with
    the search value
  2. Return first search value that matches element.
  3. Return None if whole array is iterated through without matching values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Time complexity of hash table removal

A

O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Time complexity of hash table insertion

A

O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly