Python Data Structures + Algorithms Flashcards

(69 cards)

1
Q

What is an array?

A

an ordered collection of elements

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

What is the time complexity of insertion/deletion of an array?

A

O(1)

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

What is the time complexity of searching an array?

A

O(n)

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

How do I create a list?

A

list() or my_list = [1, 2, 3]

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

How do i add elements to a list?

A

append, insert(num, idx)

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

How do I remove the last element of a list?

A

my_list.pop()

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

How do I deleted an element from a list?

A

del my_list[2]

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

What is unique about arrays in Python?

A

it is dynamic

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

What is a string?

A

an immutable sequence of characters

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

What is the time complexity of access on a string?

A

O(1)

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

What is the time complexity of concatenation on a string?

A

O(n)

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

How do I slice a string?

A

s[1:4]

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

How do I concatenate a string?

A

“a” + “b”

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

What is another name for a dictionary?

A

a hash map

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

What is a dictionary?

A

a collection of key-value pairs

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

Why do we choose dictionaries?

A

they provide efficient lookups based on keys

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

What is the access/insertion/deletion time complexity of a dictionary?

A

O(1)

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

How do I create a dictionary?

A

{} or {“apple”: 1}

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

How do I insert a new value to my dictionary?

A

dictionare[“banana”] = 2

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

What are arrays used for in leetcode?

A

problems involving sequences, searching, sorting, and dynamic programming

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

What are dictionaries used for in leetcode?

A

problems involving frequency, counting, lookings, and storing mapping between elements

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

What is a set?

A

unordered collection of unique elements

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

What is the time complexity for a set?

A

O(1)

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

How do I create a set?

A

s = (1, 2, 3) or s = set([1, 2, 3])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How do I add an element to a set?
s.add(4)
26
How do I remove an element from a set?
s.remove(2)
27
How do I check if something is in my set?
return 3 in s
28
What are sets useful for?
problems that require checking for the existence of elements, finding unique elements and set operations
29
What is a stack?
Last-In-First-Out data structure
30
What is the time complexity of push/pop/peek of a stack?
O(1)
31
How do I create a stack?
my_stack = []
32
How do I add to my stack?
my_stack.append(1)
33
How do I remove the top element of my stack?
my_stack.pop()
34
What leetcode problems are stacks used for?
problems that involve backtracking, parsing expressions, and DFS
35
What is a queue?
First-In-First-Out data structure
36
What is the time complexity of enqueue/dequeue?
O(1)
37
How do we use a queue in python?
by using from collections import deque
38
How do I instantiate a deque?
my_queue = deque()
39
How do I append to a queue?
my_q.append(1)
40
How do I pop from my queue?
my_q.popleft()
41
What leetcode problems are queues used for?
BFS and level-order traversal
42
What are linked lists?
sequence of nodes where each node contains data and a pointer to the next node
43
What is a heap?
a binary heap for min/max priority queues
44
What is the time complexity of a heap insert and pop?
O(log n)
45
How do we begin to use a heap?
import heapq
46
How do we instantiate a heap?
heap = []
47
How do we add to a heap?
heapq.heappush(heap, 3)
48
How do we pop from a heap?
heapq.heappop(heap)
49
What is the time complexity of insertion/deletion of a linked list?
O(1)
50
What is the time complexity of linked list insertion and deletion in middle or end?
O(n)
51
How do I create the linked list class?
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next
52
How do I create a head for my linked list?
head = ListNode(1)
53
How do I add a new node to my existing linked list?
head.next = ListNode(2)
54
What are linked lists useful for in leetcode problems?
dynamic memory allocation, insertion/deletion at arbitrary positions, and representing sequences where size is not known beforehand
55
What is a graph?
collection of nodes connected by edges. can be directed, undirected, weighted or unweighted
56
What is an example of a graph in python?
graph = {0: [1, 2], 1: [0], 2: [0]}
57
What is a Binary Tree?
a hierarchical data structure consisting of nodes connected by edges
58
What is different about a Binary Search Tree?
it maintains a sorted order
59
What is the time complexity of access/search/insert/delete of a balanced BST?
O(log n)
60
What is the worst case time complexity of a BST
when it is unbalances; O(n)
61
How do we create a tree class?
class TreeNode: def __init___(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right
62
How do we instantiate a tree?
root = TreeNode(5)
63
How do we add a new node to a tree?
root.left = TreeNode(3)
64
What are trees used for in leetcode problems?
problems involving searching, sorting, hierarchical relationships, lookups
65
What does heap allow efficient retrieval of?
the smallest or largest element
66
What are heaps useful for in leetcode problems?
finding the k-th smallest/largest element, scheduling, Dijkstra's
67
What is the space complexity of graphs?
O(V + E)
68
What are leetcode problems are graphs useful for?
relationships between entities, pathfinding, network analysis
69