Data Types
Absolutely β knowing your core data types inside and out is essential for LeetCode and coding interviews. These are the types youβll see constantly and will use to build efficient solutions.
Hereβs a curated list of the must-know data types for LeetCode β including how to use them, when to choose them, and a one-liner to remember what theyβre best at.
π§ LeetCode Data Types You Should Know (Python)
β
1. int
β Whole numbers
Used for counting, math, binary, etc.
n = 42
π§ͺ Use cases:
-
Loops, math, binary search, bit manipulation
β
2. float
β Decimal numbers
Used rarely, but important for precision-sensitive problems.
pi = 3.14
π§ͺ Watch out for rounding errors.
β
3. str
β Strings
π₯ High-frequency on LeetCode
s = "leetcode"
π§ͺ Know:
-
s[::-1]
β reverse -
s.split()
β split by whitespace -
s.isalnum()
β alphanumeric check -
ord(c)
,chr(n)
β ASCII value
β
4. list
β Ordered, mutable collection
π₯ Most used structure in LeetCode
arr = [1, 2, 3]
π§ͺ Know:
-
arr.append(x)
-
arr.pop()
,arr.pop(0)
-
arr[::-1]
-
arr.sort()
vssorted(arr)
-
Slicing, binary search, sliding window
β
5. tuple
β Ordered, immutable collection
Used for keys in sets/dicts or returning multiple values.
t = (1, 2)
π§ͺ Know:
-
x, y = t
-
Useful as hashable keys:
visited[(x, y)] = True
β
6. set
β Unordered, unique items
π Great for fast lookup and duplicates
s = set([1, 2, 3])
π§ͺ Know:
-
x in s
β O(1) -
set(list)
removes duplicates -
Set operations:
union
,intersection
,difference
β
7. dict
β Key-value pairs
π₯ Critical for hash maps / fast lookups
d = {"a": 1, "b": 2}
π§ͺ Know:
-
d.get(k, default)
-
defaultdict(list)
-
Frequency count:
Counter(s)
-
Keys/values/items iteration
β
8. collections.deque
β Double-ended queue
β‘ Fast append
and popleft()
β great for BFS
from collections import deque
q = deque([1, 2])
q.append(3)
q.popleft()
β
9. heapq
/ Min Heap (via list)
π‘ Use for priority queue, top K elements
import heapq
h = [5, 3, 8]
heapq.heapify(h)
heapq.heappop(h)
π§ͺ Python only has min-heaps β invert values for max-heap.
β
10. collections.Counter
β Fast frequency counting
from collections import Counter
c = Counter("aabbcc")
π§ͺ Useful for:
-
Anagram check
-
Majority element
-
Multiset behavior
π§ TL;DR: Memorize These for LeetCode
Type | What it's good for |
---|---|
int |
Counting, math |
float |
Precision, rarely needed |
str |
Manipulation, parsing, search |
list |
Arrays, stacks, sliding window |
tuple |
Hashable keys, return multiple items |
set |
Uniqueness, fast lookup |
dict |
Key-value, counting, hash maps |
deque |
BFS, queue (O(1) pops) |
heapq |
Priority queue, top K problems |
Counter |
Fast frequency counting |
Would you like:
-
Anki cards for this?
-
Daily drills where you implement key operations from memory?
-
A printable reference sheet or PDF?
No Comments