Professional Documents
Culture Documents
05 Challenge 1 - Big (O) of Nested Loop With Addition - Data Structures For Coding Interviews in C#
05 Challenge 1 - Big (O) of Nested Loop With Addition - Data Structures For Coding Interviews in C#
• Problem statement
• Code snippet
Problem statement#
Compute the Big O complexity of the code snippet given below. It is better
to solve it on a piece of paper, and then see if your answer matches the
correct option!
Code snippet#
1 namespace Chapter_1
2 {
3 class Challenge_1
4 {
5 static void Main(string[] args)
6 {
7 int n = 10;
8 int sum = 0;
9 float pie = 3.14F;
10 for (int i = 0; i < n; i += 3) // O(n/3)
11 {
12 Console.WriteLine(pie); // O(n/3)
13 for (int j = 0; j < n; j += 2) // O((n/3)*(n/2))
14 {
15 sum += 1; // O((n/3)*(n/2))
16 Console.WriteLine(sum); // O((n/3)*(n/2))
17 }
https://www.educative.io/courses/data-structures-interviews-cs/m2MMRvzkxDO 1/3
06/11/2021, 20:53 Challenge 1: Big (O) of Nested Loop with Addition - Data Structures for Coding Interviews in C#
17 }
18 }
19 }
20 }
21 }
If you have computed the time complexity of the code snippet above,
answer the following question, and see if your result matches the correct
answer!
A) O(n)
B) O(nlog3 n)
C) O(log3 n)
D) O(n2 )
Submit Answer
Reset Quiz
https://www.educative.io/courses/data-structures-interviews-cs/m2MMRvzkxDO 2/3
06/11/2021, 20:53 Challenge 1: Big (O) of Nested Loop with Addition - Data Structures for Coding Interviews in C#
Back Next
Completed
Report an Issue
https://www.educative.io/courses/data-structures-interviews-cs/m2MMRvzkxDO 3/3