Loading workspace environment...
A gentle, beginner-friendly introduction to why some code runs in milliseconds while other code takes hours.
const items = ["First", "Second", "Third", "Fourth", "Fifth"];
// 1. O(1) Constant Time: Instant direct index lookup
const first = items[0];
console.log("O(1) Lookup:", first);
// 2. O(N) Linear Time: Must visit each item in sequence
function findItem(list, target) {
for (let i = 0; i < list.length; i++) {
if (list[i] === target) return "Found at index " + i;
}
return "Not found";
}
console.log("O(N) Search:", findItem(items, "Fourth"));O(1) Lookup: First
O(N) Search: Found at index 3items[0]Direct address calculation in memory (O(1)).for (let i = 0; i < list.length; i++)Visits up to N items in worst-case (O(N)).// Believing time complexity measures seconds on a stopwatch// Time complexity measures the rate of growth of operations as data size N increasesArchitectural Explanation: Seconds vary depending on computer speed; Big-O measures the mathematical growth rate.
Look at the function below. Does it run in O(1) constant time or O(N) linear time? Print your answer.