Arrays Flashcards

1
Q

Time complexity to access an element in array?

A

O(1)

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

Time complexity to search in array/list?

A

O(n)

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

Time complexity of search in a sorted array?

A

O(log(n))

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

Time complexity to Insert an item in array/list?

A

O(n) - Insertion, in the middle of an array, would require shifting all the subsequent elements to the right by one and that takes O(n)

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

Time complexity to insert an element at the end of an array/list?

A

O(1)

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

Time complexity to remove an element from the middle of an array/list?

A

O(n) - Removal would require shifting all the subsequent elements to the left by one and that takes O(n)

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

Time complexity to remove an element from the end of an array/list?

A

O(1) - Special case of removal where no other element needs to be shifted.

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

Advantages of Arrays

A
  1. Storing multiply elements of the same kind w/ one single variable name
  2. Accessing elements if fast if you have the index
  3. Arrays are better at processing many values easily and quickly
  4. Sorting and searching the values is easier in arrays
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Disadvantages of Arrays

A
  1. Addition/removal of elements to/from the middle of the array is slow because the remaining elements need to be shifted to accomodate new/missing elements (An exception to this is if the position to be inserted/removed is at the end of the array.)
  2. Depending on the language, arrays are fixed in size. it cannot alter its size after initialization. If an insertion causes the total number of elements to exceed the size, a new array has to be allocated and the existing elements have to be copied over. The act of creating a new array and transferring elements over takes O(n) time.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a Subarray?

A

A range of contiguous values within an array.

Example: given an array [2, 3, 6, 1, 5, 4], [3, 6, 1] is a subarray while [3, 1, 5] is not a subarray.

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

What is a Subsequence?

A

A sequence that can be derived from the given sequence by deleting some or no elements without changing the order of the remaining elements.

Example: given an array [2, 3, 6, 1, 5, 4], [3, 1, 5] is a subsequence but [3, 5, 1] is not a subsequence.

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

What is an Array?

A

Arrays hold values of the same type at contiguous memory locations. In an array, we’re usually concerned about two things - the position/index of an element and the element itself

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