Adarsh Khatri

~ writings

Running in O(1)

codingalgorithmsleetcodeprogramming
Running in O(1)

What Does O(1) Actually Mean?

Big O notation describes how the time (or memory) required for an operation grows as the input size increases.

For example:

  • O(n) means the time grows linearly with input size.
  • O(log n) means the time grows slowly, even as the input gets large.
  • O(1) means the time stays constant, no matter how big the input is.

In layman’s terms, imagine you're in a room looking for your friend among a group of people. In the first scenario, you know exactly where your friend is—that’s O(1). In the second scenario, you don’t know where your friend is, so you have to check each person’s face and location—that’s O(n).

Why Does It Matter?

Performance isn't just a metric you look at in benchmarks—it directly affects how users experience your application. Faster operations mean better responsiveness, which leads to better user satisfaction.

Lessons from Core Java Volume I

Reading Core Java Volume I has deepened my understanding of Java’s standard library, especially the java.util collections framework. The book provides great insights into how common data structures like HashMap, HashSet, LinkedList, and ArrayList work under the hood.

ArrayList<String> menuItems = new ArrayList<>();
menuItems.get(index); // O(1)

LinkedList<String> menuItems = new LinkedList<>();
menuItems.get(index); // O(n)

Both classes let you store a list of items, but there's a big difference in how you access them. Accessing an element by index in an ArrayList is O(1), while doing the same in a LinkedList is O(n), because it has to traverse the list to reach the index.