Skip to solution
easyDSA

What is the time complexity of common operations on arrays, hash maps, and balanced trees?

753 views
01

Understand the problem

Explain operation complexities.

complexity
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Array: O(1) index, O(n) search/insert-middle. Hash map: O(1) average insert/lookup, O(n) worst. Balanced BST: O(log n) search/insert/delete and gives sorted order, which hash maps don't.

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

Picking the right structure
Run Playground
import bisect

# Array / list: O(1) index, O(n) search
arr = [10, 20, 30]
print(arr[1])              # 20    O(1)
print(20 in arr)           # True  O(n)

# dict (hash map): O(1) average lookup, unordered
seen = {"a": 1, "b": 2}
print(seen["a"])           # 1     O(1) avg

# Ordered / range queries: Python has no built-in BST; a sorted list
# + bisect gives O(log n) search. (3rd-party: sortedcontainers.SortedDict)
keys = [1, 3, 5]
print(keys[bisect.bisect_left(keys, 3)])   # 3   search/successor, O(log n)
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 12 of 127 decoded in the Data Structures & Algorithms track. One more won't hurt.

Back to track