DS, OOP , Algorithms Exam Preb

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/92

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 12:51 AM on 6/16/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

93 Terms

1
New cards

Big O

Describes how an algorithm's time or space grows as input size grows

2
New cards

O(1)

Constant time. Runtime does not grow with input size

3
New cards

O(n)

Linear time. Runtime grows directly with input size

4
New cards

O(n²)

Quadratic time. Usually caused by nested loops over the same input

5
New cards

O(log n)

Logarithmic time. Usually caused by repeatedly cutting the input in half

6
New cards

O(n log n)

Usually appears in efficient sorting algorithms like merge sort, heap sort, and C++ sort

7
New cards

Drop constants in Big O

O(2n) becomes O(n), and O(10n) becomes O(n)

8
New cards

Drop smaller terms

O(n² + n) becomes O(n²)

9
New cards

Hash map lookup

Average O(1), but uses extra space

10
New cards

Brute force Two Sum

Checks every pair, so time complexity is O(n²)

11
New cards

Optimized Two Sum

Uses a hash map, so average time complexity is O(n)

12
New cards
i += 2 in a loop
Still O(n), because n/2 becomes O(n)
13
New cards
O(log n)
Usually happens when the loop repeatedly multiplies or divides the value
14
New cards
Nested loops
Multiply their complexities
15
New cards
Separate code blocks
Add their complexities, then keep the biggest term
16
New cards
O(nm)
Used when one loop depends on n and the nested loop depends on m
17
New cards
O(n log n)
Common for efficient sorting like C++ sort()
18
New cards
vector push_back
O(1) amortized
19
New cards
unordered_map loop
O(n) average time if each operation is O(1)
20
New cards
Amortized O(1)
Most operations are O(1), but sometimes one operation is slower. When averaged over many operations, the cost is still O(1).
21
New cards
vector push_back complexity
In C++, vector.push_back() is O(1) amortized because adding is usually constant time, but sometimes the vector resizes and copies elements.
22
New cards
Why vector push_back is not always exactly O(1)
When the vector is full, it may allocate bigger memory and move or copy old elements, which can take O(n). But this happens only sometimes.
23
New cards
Same Big O = same speed?
No. Two algorithms can both be O(n), but one can still be faster in real life because Big O ignores constants and smaller details.
24
New cards
Example of same Big O but different speed
A loop that checks n items once and a loop that checks n items with 10 operations each are both O(n), but the first is usually faster.
25
New cards
What Big O tells us
Big O tells us how runtime grows as input grows. It does not tell the exact running time.
26
New cards
Why we drop constants
O(2n), O(10n), and O(n) are all written as O(n) because Big O focuses on growth rate, not exact operations.
27
New cards
Big O compares growth, not exact speed
This means two algorithms can have the same Big O but still perform differently in practice.
28
New cards

Bitmasking

A technique where one integer stores multiple yes/no values using its binary bits.

29
New cards

Bitmask

The integer that holds the combined flags.

30
New cards

Bit flag

One bit inside the bitmask that represents one option, permission, or state.

31
New cards

Bitwise operators for bitmasking

Use | to add a flag, & to check a flag, and & ~ to remove a flag.

32
New cards
Stack clue
If the problem needs the most recent item first, use a stack.
33
New cards
Valid Parentheses pattern
Push opening brackets. For every closing bracket, check whether it matches the most recent opening bracket.
34
New cards
Why Valid Parentheses uses stack
Because brackets must close in reverse order: the last opened bracket must be closed first.
35
New cards
LIFO
Last In, First Out. The last item added is the first item removed.
36
New cards
stack.push(x)
Adds x to the top of the stack.
37
New cards
stack.top()
Returns the item at the top of the stack without removing it.
38
New cards
stack.pop()
Removes the item at the top of the stack.
39
New cards
stack.empty()
Returns true if the stack has no elements.
40
New cards
Stack runtime error
Calling top() or pop() on an empty stack can cause a runtime error.
41
New cards
Safe stack rule
Before using st.top() or st.pop(), check st.empty().
42
New cards
Opening bracket in Valid Parentheses
If the character is '(', '[', or '{', push it into the stack.
43
New cards
Closing bracket in Valid Parentheses
If the character is ')', ']', or '}', first check if the stack is empty, then check if the top matches.
44
New cards
Valid Parentheses final check
After processing all characters, return true only if the stack is empty.
45
New cards
Why return false when stack is empty on a closing bracket
Because there is no previous opening bracket to match with this closing bracket.
46
New cards
Most recent opening bracket
The bracket on top of the stack. It is the one that must match the current closing bracket.
47
New cards
break vs continue
break stops the entire loop, while continue skips only the current iteration and moves to the next one.
48
New cards
stoi() in Baseball Game
Only call stoi() after checking special operations like C, D, and +. The final else means the operation must be a number.
49
New cards
Vector as record/history
A vector can work like a history list when we need to add the newest value and remove the most recent value using push_back() and pop_back().
50
New cards
Algorithm
A step-by-step strategy used to solve a problem.
51
New cards
Data structure vs algorithm
A data structure stores/organizes data, while an algorithm is the strategy used to solve the problem.
52
New cards
Brute force
Try all possible answers directly. It is usually simple but often expensive.
53
New cards
Brute force usefulness
Brute force is useful because it proves you understand the problem before optimizing.
54
New cards
Backtracking
An algorithm technique that builds an answer step by step, tries choices, explores them, then undoes the choice to try another.
55
New cards
Backtracking rhythm
Choose, explore, undo.
56
New cards
Backtracking clue
Use backtracking when the problem says generate all, all permutations, all combinations, all valid arrangements, or all paths.
57
New cards
State
The current situation of the algorithm, such as the current node, current cell, current partial answer, or used values.
58
New cards
Undo in backtracking
Removing the last choice so the algorithm can try another choice.
59
New cards
Pruning
Skipping a path early because it already violates a rule or cannot lead to a useful answer.
60
New cards
Permutation
An arrangement where order matters. For example, [1,2] and [2,1] are different permutations.
61
New cards
Combination
A selection where order does not matter. For example, [1,2] and [2,1] are the same combination.
62
New cards
Subsets pattern
For each item, choose take or skip. Total subsets for n items is 2^n.
63
New cards
Permutations pattern
Choose one unused item each time. For n items, total permutations is n!.
64
New cards
DFS
Depth First Search. It explores deeply first, usually using recursion or a stack.
65
New cards
DFS clue
Use DFS for exploring connected nodes, trees, graphs, grids, paths, and backtracking-style problems.
66
New cards
BFS
Breadth First Search. It explores level by level, usually using a queue.
67
New cards
BFS clue
Use BFS for shortest path, minimum steps, level order traversal, nearest target, and spread step by step problems.
68
New cards
Why BFS finds shortest path
In an unweighted graph, BFS explores all nodes 1 step away before 2 steps away, so the first time it reaches a node is the shortest path.
69
New cards
Connected component
A separate group of connected nodes.
70
New cards
Island in grid problem
A connected group of land cells, usually 1s, connected up/down/left/right.
71
New cards
Visited in DFS/BFS
Marks nodes or cells already processed so we do not count them again or loop forever.
72
New cards
Frequency counting
Count how many times each item appears.
73
New cards
Frequency counting clue
Use frequency counting for anagrams, most frequent, character counts, duplicates with counts, or how many times something appears.
74
New cards
Lookup
Check quickly if something exists, usually using a hash set or hash map.
75
New cards
Lookup clue
Use lookup when asking: have I seen this before? Does this value exist?
76
New cards
Hash set use
Use a hash set when you only need to know whether a value exists.
77
New cards
Hash map use
Use a hash map when you need key to value, such as number to index or character to count.
78
New cards
Two pointers
Use two positions to scan data, often from both ends or at different speeds.
79
New cards
Two pointers left-right clue
Use left-right pointers for palindrome, sorted pair problems, reverse, or comparing both ends.
80
New cards
Slow and fast pointers
Use two pointers where slow moves 1 step and fast moves 2 steps, often for linked list middle or cycle detection.
81
New cards
Sliding window
A two-pointer pattern for continuous subarrays or substrings where the window expands and shrinks.
82
New cards
Sliding window clue
Use sliding window for longest substring, shortest substring, subarray, continuous block, at most K, or without repeating.
83
New cards
Sliding window movement
Right expands the window, left shrinks or fixes the window.
84
New cards
Prefix sum
A running total from the start of the array to the current index.
85
New cards
Prefix sum clue
Use prefix sum for range sum, sum between indexes, subarray sum, or many sum queries.
86
New cards
Prefix sum formula
sum from i to j = prefix[j] - prefix[i - 1].
87
New cards
Greedy
Choose the best local option at each step without going back.
88
New cards
Greedy clue
Use greedy when a local best choice safely leads toward the global best answer, like keeping the lowest stock price so far.
89
New cards
Dynamic Programming
An algorithm technique for repeated subproblems where we store previous answers to avoid recalculating.
90
New cards
DP clue
Use DP for number of ways, maximum/minimum over choices, can/cannot reach, repeated subproblems, or best answer up to index i.
91
New cards
Sorting clue
Use sorting when order makes the problem easier, such as grouping anagrams, handling duplicates, intervals, or using two pointers.
92
New cards
Binary search
Search by cutting the search space in half each time.
93
New cards
Binary search clue
Use binary search when the array is sorted or the answer space is monotonic.