Algorithm and Data Structures Flashcards

1
Q

What is a data structure?

A

A data structure is a way of storing and organizing data according to a certain format or structure, allowing us to access and manipulate the data in a certain way

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

Examples of data structures

A

tables to display schedules

a novel stores and organizes texts in paragraphs

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

Why are data structures important?

A

Data structures are a crucial part of computer programming as programming relies on our ability to store data, access that data, and manipulate that data to create the desired outcome.

Therefore, it is a top priority that engineers leverage data structures optimally to organize and manipulate data in an efficient and meaningful way

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

What are the 4 primary data structures in Python3?

A
  1. List
  2. Tuple
  3. Dictionary
  4. Set
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

So when thinking about a program… the data structure should be top of mind in the process of mapping out the path to success

A
  1. What are we trying to achieve?
  2. What data will we need?
  3. What data do we have already?
  4. What will we need to do with the data?
  5. What are our challenges?
  6. How is this data stored?
  7. How will we interact with the data?
  8. How will we store the data?
  9. How will we access the data?
  10. How will we manipulate the data to create our desired outcome?
  11. How will we overcome our challenges?

what are the conditions needed to produce an optimal output from the given input?

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

What is a list?

A

a data structure that allows us to store elements of different data types in one container, linearly. Each element is indexed from 0 and up

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

Are lists mutable?

A

Yes, lists are mutable.

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

Are lists ordered?

A

Yes, lists are ordered linearly with each element at a specific index. The first element is at index 0, the next is at index 1, and so forth

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

What are two ways to merge two lists together?

A
  1. using the + operator

merged_list = list_1 + list_2

  1. using the extend() property of a list to add the elements of one list at the end of another:

merged_list = list_1.extend(list_2)

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

two ways of adding elements to a list

A
  1. the append() method adds an element to the end of a list
    list. append(new_element)
  2. the insert() method adds an element at a specified index within a list
    list. insert(1, new_element)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

two ways of removing an element from a list

A
  1. the pop() method removes the last element from a list, and returns that element
    list. pop()
  2. the remove() method removes a specified element from a list
    list. remove(this_element)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is list slicing?

A

provides a sublist of the list being sliced.

the syntax for list slicing is:

list[starting_index, ending_exclusive_index, steps]

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

How can we verify the existence of an element in a list?

A

using the in operator

element_hello in list

this will return True if element_hello is in the list
and False if not in the list

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

What is list comprehension?

A

a technique that uses a for loop to create a new list from an existing list

since the result is always a NEW list, it’s good practice to assign list comprehension to a new variable

list comprehension is enclosed in square brackets

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

What is a tuple?

A

a tuple is similar to a list in that it can contain different data types and is ordered linearly and 0 indexed

a tuple is different because it is enclosed in parentheses AND is immutable. Meaning the tuple cannot be altered. BUT, the elements of a tuple are mutable and can be altered

since tuples are immutable, it is NOT possible to add or delete elements from an existing tuple

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

What is a dictionary?

A

an unordered data structure that stores data as key-value pairs, where each unique key is an index which holds the value associated with it

key:value pairs are accessed in a random, unordered manner

17
Q

What is a set?

A

an unordered collection of data items

18
Q

When would I use a set?

A

when you need to keep track of the existence of items

a set doesn’t allow duplicates. So you can convert another data structure to a set to remove duplicates

19
Q

Set Theory Operations

A

are the operations of a set: union, intersection, difference

20
Q

What is a Union?

A

a collection of all unique elements from both sets

syntax is the pipe, ‘|’, operator or the union() method

21
Q

What is the intersection?

A

a collection of unique elements which are common between two sets

syntax is the ‘&’ operator or the intersection() method

22
Q

What is the difference?

A

a collection of all the unique elements present in the first set and not present in the second set

syntax: - operator, or difference() method

23
Q

Data Structure Conversion: Explicit Conversion

A

syntax: destination_structure_name(source_structure_object)

where destination structure is the data structure we want to convert to

and source_structure is the object we want to convert

24
Q

Converting to a list

A

we use the listt() constructore

when converting a dictionary to a list, only the keys will be converted to a list

25
Q

Converting to a Tuple

A

any data structure can be converted to a tuple using the tuple() constructor

In the case of converting a dictionary, only the keys will be converted to a tuple

26
Q

Converting to a Set

A

use the set() constructor to convert any data structure to a set

in the case of a dictionary, only the keys will be converted to a set

27
Q

converting to a dictionary

A

the dict() constructor can be used to a convert a tuple with a length of two to a dictionary, since it reuqires a pair of data to serve its key: value format

28
Q

What is the syntax for filter?

A

filter(function, iterable)

29
Q

What is a Stack? And When is it useful?

A

Imagine a stack of books

to build this stack of books we add the first book to the bottom, then the next book on top of the first, and so forth.

To retrieve a book from the stack, we remove a book from the top one by one until we retrieve the book we were seeking

30
Q

What are the Stack operations?

A

Push, Pop, Peek

Push: inserts an element onto a stack as the new last/top element of the stack

Pop: removes the top/last element from the stack

Peek: ask’s the Stack, ‘What is the top element?’

31
Q

What are the advantages of a Stack data structure?

A

32
Q

What are the advantages of a Stack data structure?

A

33
Q

What are the uses of stacks?

A

Compilers and Parsers leverage stacks

Activation records: use stacks to keep track of the procedure activities during runtime

34
Q

What are the uses of stacks?

A

Compilers and Parsers leverage stacks

Activation records: used to keep track of the procedure activities during runtime of a program. Activation record is a fundamental part of programming languages and is implemented using a stack. When the function is called, an activation record is created for it and keeps track of parameters, local variables, return address, static and dynamic links, and the return value.

Web Browsers: use a stack to keep track of URLs you have visited. When you visit a page it adds it to the top and the stack. When you hit the back button the stack is popped and the previous URL you visited is accessed

To implement other Data Structures: Stacks are used to implement searches in Graphs and Trees, which are other complex data structures.

35
Q

What are the different types of Linked Lists?

A

Singly
Doubly
Circular

36
Q

What are the tradeoffs between an Array and a Linked List?

A

Insertion/Deletion: since an array has contiguous memory, in an array, you have to shift all the other elements toward the end(insertion) or the beginning(deletion). This makes an array less efficient than a Linked List for this use case. The time complexity is O(n) for an array and O(1) for a linked list

Accessing: since an array has contiguous memory, accessing an element at a specific index is more efficient than accessing nth elements in a linked list.

37
Q

What is a linked list?

A

a linked list is a data structure made up of nodes. Each node has two components: 1) data 2) next

the data component allows the node to store data

the next component is a pointer that points its node to the next node

the start of the linked list is the head, which starts at the beginning of the linked list. To access and element in a linked list we always begin at the head and traverse the linked list from there.

the end of the linked list is what we call Null or None (in Python). The last node in the linked list points to a null object, and that tells you that its the end of the linked list