Skip to content

Commit ec815cd

Browse files
majestic-owl448Ksound22jdwilkin4
authored
feat(curriculum): add working with common data structures lectures (freeCodeCamp#61747)
Co-authored-by: Kolade Chris <[email protected]> Co-authored-by: Jessica Wilkins <[email protected]>
1 parent f4307c7 commit ec815cd

File tree

7 files changed

+1377
-46
lines changed

7 files changed

+1377
-46
lines changed

curriculum/challenges/_meta/lecture-working-with-common-data-structures/meta.json

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,31 @@
55
"blockType": "lecture",
66
"blockLayout": "challenge-list",
77
"superBlock": "full-stack-developer",
8-
"challengeOrder": [{ "id": "68420c314cdf5c6863ca8330", "title": "Step 1" }],
8+
"challengeOrder": [
9+
{
10+
"id": "68420c314cdf5c6863ca8330",
11+
"title": "What Is an Algorithm and How Does Big O Notation Work?"
12+
},
13+
{
14+
"id": "6895d06b5968736797c408e3",
15+
"title": "What Are Good Problem-Solving Techniques and Ways to Approach Algorithmic Challenges?"
16+
},
17+
{
18+
"id": "6895d06b5968736797c408e4",
19+
"title": "How Do Dynamic Arrays Differ From Static Arrays?"
20+
},
21+
{
22+
"id": "6895d06b5968736797c408e5",
23+
"title": "How Do Stacks and Queues Work?"
24+
},
25+
{
26+
"id": "6895d06b5968736797c408e6",
27+
"title": "How Do Singly Linked Lists Work and How Do They Differ From Doubly Linked List?"
28+
},
29+
{
30+
"id": "6895d06b5968736797c408e7",
31+
"title": "How Do Maps, Hash Maps and Sets Work?"
32+
}
33+
],
934
"helpCategory": "Python"
10-
}
35+
}
Lines changed: 158 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,249 @@
11
---
22
id: 68420c314cdf5c6863ca8330
3-
# title needs to be updated to correct title when lectures are finalized
4-
title: Working with Common Data Structures
3+
title: What Is an Algorithm and How Does Big O Notation Work?
54
challengeType: 19
6-
# dashedName needs to be updated to correct title when lectures are finalized
7-
dashedName: lecture-working-with-common-data-structures
5+
dashedName: what-is-an-algorithm-and-how-does-big-o-notation-work
86
---
97

108
# --description--
119

12-
Watch the video or read the transcript and answer the questions below.
10+
Every computer program that runs on your device has a specific set of instructions, which are executed in a specific order to complete a task.
11+
12+
The task could be sorting a set of numbers, modifying an image, tracking inventory, or even running your favorite video game.
13+
14+
This is where algorithms come into play. An **algorithm** is a set of unambiguous instructions for solving a problem or carrying out a task.
15+
16+
You can think of algorithms as "recipes". When you cook, recipes list all the ingredients that you'll need, and provide step by step instructions on how to prepare a dish.
17+
18+
Equivalently, you can think of algorithms as "recipes" that tell computers exactly what should be done and how to do it.
19+
20+
Algorithms have two key characteristics:
21+
22+
* They cannot continue indefinitely. They must finish in a finite number of steps.
23+
24+
* Each step must be precise and unambiguous.
25+
26+
27+
They may have zero, one, or more inputs, and generate one or more outputs.
28+
29+
The steps of an algorithm are independent from any programming language.
30+
31+
But to actually make them run on a computer, you need to implement them in a programming language, like Python or JavaScript.
32+
33+
If an algorithm is correct, the output for any valid input should match the expected output.
34+
35+
In addition to being correct, algorithms should also be efficient.
36+
37+
Algorithm efficiency can be measured in terms of how long they take to run and how much space they require in memory to complete the task.
38+
39+
Knowing an algorithm's efficiency is very important because it gives you an idea of how well it will perform as the input size grows.
40+
41+
For example, sorting 15 integers is not the same as sorting 1 million integers.
42+
43+
As the process grows in size and complexity, if the algorithm is not efficient enough to handle it, you might end up with a very slow computer program that may even crash the entire system.
44+
45+
That's why it's very important to develop and choose the most efficient algorithms possible.
46+
47+
This is where Big O notation becomes very important.
48+
49+
Big O notation describes the worst-case performance, or growth rate, of an algorithm as the input size increases.
50+
51+
The growth rate of an algorithm refers to how the resources it requires increase as the input size grows.
52+
53+
Big O notation focuses on the worst-case performance because this case is very important to understand how efficient the algorithm can be, even in the worst case scenario, regardless of the input.
54+
55+
Going back to our sorting example, sorting 1 million integers should intuitively take more time and resources than sorting 15 integers.
56+
57+
But how much more?
58+
59+
This really depends on the algorithm that you choose to sort them.
60+
61+
Big O notation will not give you an exact number to describe the algorithm's efficiency, but it will give you an idea of how it scales as the input size grows, based on the number of operations performed by the algorithm.
62+
63+
In Big O notation, we usually denote input size with the letter `n`. For example, if the input is a list, `n` would denote the number of elements in that list.
64+
65+
Constant factors and lower-order terms are not taken into account to find the time complexity of an algorithm based on the number of operations. That's because as the size of `n` grows, the impact of these smaller terms in the total number of operations performed will become smaller and smaller.
66+
67+
The term that will dominate the overall behavior of the algorithm will the term with `n`, the input size.
68+
69+
For example, if an algorithm performs `7n + 20` operations to be completed, the impact of the constant `20` on the final result will be smaller and smaller as `n` grows. The term `7n` will tend to dominate and this will define the overall behavior and efficiency of the algorithm.
70+
71+
Another example would be an algorithm that takes `20n² + 15n + 7` operations to be completed. The term `20n²` will tend to dominate as `n` grows, so this algorithm would have a quadratic time complexity because the dominant term has ``.
72+
73+
Quadratic time complexity is one of many different types of time complexities that you can find in the world of algorithms.
74+
75+
Let's learn about some of the most common ones.
76+
77+
**`O(1)`** is known as "Constant Time Complexity". When an algorithm has constant time complexity, it takes the same amount of time to run, regardless of input size.
78+
79+
For example, checking if a number is even or odd will always take the same amount of time, regardless of the number itself.
80+
81+
```python
82+
def check_even_or_odd(number):
83+
if number % 2 == 0:
84+
return 'Even'
85+
else:
86+
return 'Odd'
87+
```
88+
89+
**`O(log n)`** is known as "Logarithmic Time Complexity". This means that the time required by the algorithm increases slowly as the input size grows. This is common in problems in which the size of the problem is repeatedly reduced by a constant fraction.
90+
91+
For example, a popular search algorithm called Binary Search has `O(log n)` worst-case time complexity. This is because it eliminates half of the remaining elements in each comparison, which makes it more efficient overall.
92+
93+
**`O(n)`** is known as "Linear Time Complexity". The running time of algorithms with this time complexity increases proportionally to the input size.
94+
95+
For example, a `for` loop that iterates over all the elements of a list will perform more iterations as the number of list elements increases. If the list is doubled in size, the number of operations will approximately double as well.
96+
97+
```python
98+
for grade in grades: # grades is a list.
99+
print(grade)
100+
```
101+
102+
**`O(n log n)`** is known as "Log-Linear Time Complexity". This is a common time complexity of efficient sorting algorithms, like Merge Sort and Quick Sort.
103+
104+
**`O(n²)`** is known as "Quadratic Time Complexity". The running time of these algorithms increases quadratically relative to the input size, which is generally not efficient for real-world problems.
105+
106+
Nested loops are a common example of quadratic time complexity. The inner loop will perform `n` iterations for each one of the `n` iterations of the outer loop, resulting in `n` squared iterations.
107+
108+
```python
109+
for i in range(n):
110+
for j in range(n):
111+
print("Hello, World!")
112+
```
113+
114+
Other time complexities include "Exponential Time Complexity", denoted as `O(2^n)`, and "Factorial Time Complexity", denoted as `O(n!)`. Both are inefficient for real-world scenarios.
115+
116+
In this graph, you can compare the growth of the mathematical functions that represent the most common time complexities. Think of the x-axis (horizontal) as the input size and the y-axis (vertical) as the running time of the algorithm.
117+
118+
You can see that the Quadratic Time Complexity (`O(n²)`) (yellow) grows much faster than the other ones, while the Constant Time Complexity (`O(1)`) (red) stays constant, even if the input gets larger.
119+
120+
<img src="https://cdn.freecodecamp.org/curriculum/lecture-transcripts/what-is-an-algorithm-and-how-does-big-o-notation-work-1.png" alt="graph comparing time complexity">
121+
122+
Great. So far, you've learned about Big O notation in terms of time requirements, but this notation can also be applied to the context of space requirements.
123+
124+
In this context, it describes how the memory space required by the algorithm grows as the input size grows.
125+
126+
Algorithms with "Constant Space Complexity" `O(1)` always require a constant amount of memory space, even as the input gets larger.
127+
128+
An example would be an algorithm that only creates and stores a few variables in memory.
129+
130+
In contrast, the space required by algorithms with "Linear Space Complexity" `O(n)` increases proportionally as the input size grows.
131+
132+
An example of this would be an algorithm that creates and stores a copy of a list of length `n`.
133+
134+
And finally, the space requirements of an algorithm with "Quadratic Space Complexity" `O(n²)` increase quadratically as the input size grows.
135+
136+
An example of this would be creating a 2D matrix, where the dimensions are determined by the input size, storing all possible pairs.
137+
138+
Algorithms are the building-blocks of computer programs, while Big O notation is a powerful framework for analyzing how efficient they are, based on how their time and space requirements in the worst-case scenario scale as the input size grows. Understanding their efficiency is very important for developing software that works efficiently in real-world scenarios.
13139

14140
# --questions--
15141

16142
## --text--
17143

18-
Question 1
144+
Which of the following best describes an algorithm?
19145

20146
## --answers--
21147

22-
Answer 1.1
148+
A specific programming language used to write code.
23149

24150
### --feedback--
25151

26-
Feedback 1
152+
Think about what you follow when you're trying to achieve a specific task.
27153

28154
---
29155

30-
Answer 1.2
31-
32-
### --feedback--
33-
34-
Feedback 1
156+
A set of step-by-step instructions designed to solve a problem or perform a task.
35157

36158
---
37159

38-
Answer 1.3
160+
A type of computer hardware component.
39161

40162
### --feedback--
41163

42-
Feedback 1
164+
Think about what you follow when you're trying to achieve a specific task.
43165

44166
---
45167

46-
Answer 1.4
168+
A software application used for developing and playing games.
47169

48170
### --feedback--
49171

50-
Feedback 1
172+
Think about what you follow when you're trying to achieve a specific task.
51173

52174
## --video-solution--
53175

54-
5
176+
2
55177

56178
## --text--
57179

58-
Question 2
180+
What is the primary purpose of Big O notation in the context of algorithms?
59181

60182
## --answers--
61183

62-
Answer 2.1
184+
To measure the exact time an algorithm takes to run on a specific computer in seconds.
63185

64186
### --feedback--
65187

66-
Feedback 2
188+
Think about what Big O notation helps you understand an algorithm's performance when the amount of data it processes gets very large.
67189

68190
---
69191

70-
Answer 2.2
192+
To count the total number of lines of code in an algorithm.
71193

72194
### --feedback--
73195

74-
Feedback 2
196+
Think about what Big O notation helps you understand an algorithm's performance when the amount of data it processes gets very large.
75197

76198
---
77199

78-
Answer 2.3
79-
80-
### --feedback--
81-
82-
Feedback 2
200+
To describe how the resource usage of an algorithm grows as the input size increases.
83201

84202
---
85203

86-
Answer 2.4
204+
To determine the best-case performance of an algorithm.
87205

88206
### --feedback--
89207

90-
Feedback 2
208+
Think about what Big O notation helps you understand an algorithm's performance when the amount of data it processes gets very large.
91209

92210
## --video-solution--
93211

94-
5
212+
3
95213

96214
## --text--
97215

98-
Question 3
216+
If an algorithm has a time complexity of `O(n)`, what does this mean about its performance?
99217

100218
## --answers--
101219

102-
Answer 3.1
103-
104-
### --feedback--
105-
106-
Feedback 3
220+
The algorithm's running time increases proportionally with the input size.
107221

108222
---
109223

110-
Answer 3.2
224+
The algorithm's running time remains constant regardless of the input size.
111225

112226
### --feedback--
113227

114-
Feedback 3
228+
Think about what "linear" means in terms of a direct relationship or a straight line on a graph.
115229

116230
---
117231

118-
Answer 3.3
232+
The algorithm's running time grows exponentially with the input size.
119233

120234
### --feedback--
121235

122-
Feedback 3
236+
Think about what "linear" means in terms of a direct relationship or a straight line on a graph.
123237

124238
---
125239

126-
Answer 3.4
240+
The algorithm's running time decreases as the input size gets larger.
127241

128242
### --feedback--
129243

130-
Feedback 3
244+
Think about what "linear" means in terms of a direct relationship or a straight line on a graph.
131245

132246
## --video-solution--
133247

134-
5
248+
1
135249

0 commit comments

Comments
 (0)