
Python
Learning Level
Master the theoretical foundations, memory models, and algorithmic complexity of data structures.
An Abstract Data Type (ADT) defines operations without implementation details. Multiple implementations can exist for the same ADT.
# ADT: Stack (Last-In-First-Out)
# Implementations: List-based, LinkedList-based
class ListStack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
class LinkedListStack:
def __init__(self):
self.head = None
# Different implementation, same interfaceOperations have different complexities depending on the data structure and operation.
# List - O(n) search worst case
def find_in_list(lst, target):
for item in lst: # O(n) iterations
if item == target:
return item
return None
# Dictionary - O(1) average case search
def find_in_dict(d, target):
return d.get(target) # Hash lookup, O(1)
# Benchmark example
import time
large_list = list(range(1000000))
large_dict = {i: i for i in range(1000000)}
# List search: slow
start = time.time()
result = find_in_list(large_list, 999999)
list_time = time.time() - start
# Dict search: fast
start = time.time()
result = find_in_dict(large_dict, 999999)
dict_time = time.time() - start
print(f"List: {list_time:.4f}s, Dict: {dict_time:.6f}s")
# List: 0.0450s, Dict: 0.000001s (45,000x faster!)Design decisions involve tradeoffs between speed and memory usage.
# Approach 1: Compute values on demand (low memory, slow)
def fibonacci_compute(n):
if n <= 1:
return n
return fibonacci_compute(n-1) + fibonacci_compute(n-2)
# Approach 2: Memoization (more memory, faster)
memo = {}
def fibonacci_memo(n):
if n in memo:
return memo[n]
if n <= 1:
result = n
else:
result = fibonacci_memo(n-1) + fibonacci_memo(n-2)
memo[n] = result
return result
# Approach 3: Precompute & store (high memory, instant access)
fib_table = [0, 1]
for i in range(2, 100):
fib_table.append(fib_table[-1] + fib_table[-2])| Operation | List | Dict | Set | Tuple |
|---|---|---|---|---|
| Access/Lookup | O(1)* | O(1) avg | N/A | O(1)* |
| Search | O(n) | O(1) avg | O(1) avg | O(n) |
| Insert | O(1) end / O(n) mid | O(1) avg | O(1) avg | N/A |
| Delete | O(n) | O(1) avg | O(1) avg | N/A |
| Space | O(n) | O(n) | O(n) | O(n) |
Ready to practice? Challenges | Quiz
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward