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:


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:


4. list – Ordered, mutable collection

🔥 Most used structure in LeetCode

arr = [1, 2, 3]

🧪 Know:


5. tuple – Ordered, immutable collection

Used for keys in sets/dicts or returning multiple values.

t = (1, 2)

🧪 Know:


6. set – Unordered, unique items

🔑 Great for fast lookup and duplicates

s = set([1, 2, 3])

🧪 Know:


7. dict – Key-value pairs

🔥 Critical for hash maps / fast lookups

d = {"a": 1, "b": 2}

🧪 Know:


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:


🧠 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:


Revision #1
Created 5 April 2025 22:46:12 by victor
Updated 5 April 2025 22:46:23 by victor