Lisa H.

asked • 10/15/21

Is my thought process for a big-o question correct?

For this question:


Approximate the runtime of the following code fragment, in terms of n: Write you answers in a format such as O(N^2) or O(N log N):

int sum = 0;

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= 1000000; j++) {

sum += 10;

}

}

sum+=9999;


Why is the answer O(N)? Is it because the first for-loop is O(N) and the second for-loop is O(1000000), which then gets reduced into O(1), so then since O(1) is so small, we only take O(N)? Is my thought process correct?


(Question is taken off of Practice-It from University of Washington)


1 Expert Answer

By:

Simon W. answered • 10/15/21

Tutor
5 (52)

Full Stack Software Engineer Specializing in JavaScript/TypeScript

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.