DSA Mock Interview Flashcards

1
Q

How might you determine the efficiency of an algorithm?

A

Determined by the Big O notation

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

Why is the Big O Notation important?

A

So that we can have the code as smooth as possible. Speed is important.

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

What categories of complexities are described in Big O Notations

A

Time complexity: How long code will take to execute as data increases

Space complexity: How much space is needed and how efficiently it is used

More space = less time and the other way around

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

What is O(1)

A

O(1) = Constant Time

examples:
- setting a variable once
- performing a calculation
- determining if an integer is even or odd

Basically a flat line on a graph. Easy to plan for.

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

What is O( n )

A

O( n ) = Linear Time

examples:

  • basic non-nested for loops
  • finding smallest or largest item in an unsorted array

Basically a 45 degree line on graph, still easy to plan for, scaling probably won’t run away from you.

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

What is O( n^2 )?

A

O( n^2 ) = N squared

  • basic nested for loops, bubble sort, insertion sort.

Starts to get harder to plan for as data sets increase dramatically.

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

What is O( log n )

A

O( log n ) = Log N (Logarithmic Time)

  • binary search - when you can easily eliminate large sections of data on each iteration.

Means that scaling large data sets won’t dramatically increase the output time.

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

Describe 3 differences between a linked list and an array

A

LINKED LIST:

  • Non-contiguous space requirement.
    e. i. Data doesn’t HAVE to be in same space on disk.
  • Can be more efficient for memory allocation. Easier to resize.
  • Can be less efficient for caching (not contiguous)
  • No “find by index” feature, so accessing individual elements can be slower
  • Easier to add elements (insert nodes) to various positions within list.

ARRAY:

  • Must be in same contiguous disk space.
  • Harder to resize. Also, increasing length may require moving the array in memory to a larger space.
  • Better caching - all addresses are contiguous
  • Fast access for reading when searching by index
  • Harder to insert new elements to front and within the array as you then have to shift existing elements after the new element is added.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What’s the difference between a Binary Tree and a Binary Search Tree

BST: All left descendants are less than or equal to node, and all right descendants are greater than node.

A

BST (Binary Search Tree): All left descendants are less than or equal to node, and all right descendants are greater than node

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

Q: describe the 3 types of BST traversal:

25

/ \

10 32

A

In-order (from the smallest number to the biggest) (ex: 10, 25, 32)

Visit left branch, then current node, then right branch. Visits all nodes in ascending order on a BST.

Pre-order (looks like the ““ sign) (branches first, node last) (ex: 10, 32, 25)

Visits left branch, then right branch, then current node. Root node is always last visited. Useful for language processors

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

Q: Algorithms for searching through arrays and trees

A

Linear search
Binary search
Depth-first search
Breadth-first search

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

Linear search

A

Look through an array one-by-one until you find what you are looking for

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

Binary search

A

Narrow it down by always guessing in the middle of the range

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

Depth-first search

A

Traverse as far as you can down the tree before back-tracking

e.i. check if a person related to a past royal relative has the right to be a king

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

Breadth-first search

A

Search works across the rows of a tree (the top row will be handled first, then the second row, and so on)

e.i check if multiple people from the same generation are part of the royal family

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

Q: Array sorting methods

A
Merge sort
Bubble sort
Quick sort
Selection sort
Insertion sort
Heap sort
Bucket (bin) sort
17
Q

Merge sort

A
  • DIVIDE THE UNSORTED LIST INTO N SUBLISTS, each containing 1 element (a list of 1 element is considered sorted).
  • repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.
18
Q

Bubble sort

A

REPEATEDLY SWAPPING the adjacent elements if they are in wrong order.

19
Q

Quick sort

A
  • DIVIDE AND CONQUER algorithm. It creates two empty arrays to hold elements less than the pivot value and elements greater than the pivot value, and then recursively sort the sub arrays. There are two basic operations in the algorithm, swapping items in place and partitioning a section of the array.
20
Q

Selection sort

A
  • REPEATEDLY FINDING THE MINIMUM ELEMENT (considering ascending order) from unsorted part and putting it at the beginning
21
Q

Insertion sort

A
  • Begin at the left-most element of the array and invoke Insert to INSERT EACH ELEMENT ENCOUNTERED INTO ITS CORRECT POSITION.
  • The ordered sequence into which the element is inserted is stored at the beginning of the array in the set of indices already examined.
22
Q

Heap sort

A
  • Involves preparing the list by first TURNING IT INTO A MAX HEAP (a complete Binary Tree represented as an array)
  • then repeatedly swaps the first value of the list with the last value, decreasing the range of values considered in the heap operation by one, and sifting the new first value into its position in the heap.
23
Q

Bucket (bin) sort

A
  • sorting algorithm that works by DISTRIBUTING THE ELEMENTS OF AN ARRAY INTO A NUMBER OF BUCKETS.
  • each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm.
  • sort each non-empty bucket.
24
Q

Q: What are the arrays sorting methods by speed

A

The slowest is selection, then bubble, then insertion, then shell, then merge, then heap, then quick, then quick3

25
Q

Q: What is faster than a bubble sort?

A

Insert, Shell, Heap, Merge, Quick and Quick3

26
Q

Possible coding challenge

A

Create an algorithm for counting how many times a word appears in a document.

27
Q

Computational Complexity

A

O(1) - constant time
O(log n) - logarithmic time
O(n) - linear time