<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://cramsession.net/index.php?action=history&amp;feed=atom&amp;title=Computer_Notes%2FPython%2FPython_Interview_Cheat_Sheet</id>
	<title>Computer Notes/Python/Python Interview Cheat Sheet - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://cramsession.net/index.php?action=history&amp;feed=atom&amp;title=Computer_Notes%2FPython%2FPython_Interview_Cheat_Sheet"/>
	<link rel="alternate" type="text/html" href="https://cramsession.net/index.php?title=Computer_Notes/Python/Python_Interview_Cheat_Sheet&amp;action=history"/>
	<updated>2026-08-26T18:04:56Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.1</generator>
	<entry>
		<id>https://cramsession.net/index.php?title=Computer_Notes/Python/Python_Interview_Cheat_Sheet&amp;diff=1827&amp;oldid=prev</id>
		<title>Mflavell: Created page with &quot; = High-Yield Python Patterns for Technical Interviews =  == 1. Array (List) Iteration == Forget standard while loops or range(len(arr)) unless you specifically only need the index. Python has built-in ways to make this much cleaner and less prone to off-by-one errors.  === The Enumerate Pattern (Crucial) === Whenever you need both the index and the value, always use enumerate. It shows operational maturity.  nums = [10, 20, 30] for i, num in enumerate(nums): print(f&quot;Ind...&quot;</title>
		<link rel="alternate" type="text/html" href="https://cramsession.net/index.php?title=Computer_Notes/Python/Python_Interview_Cheat_Sheet&amp;diff=1827&amp;oldid=prev"/>
		<updated>2026-08-18T01:41:48Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; = High-Yield Python Patterns for Technical Interviews =  == 1. Array (List) Iteration == Forget standard while loops or range(len(arr)) unless you specifically only need the index. Python has built-in ways to make this much cleaner and less prone to off-by-one errors.  === The Enumerate Pattern (Crucial) === Whenever you need both the index and the value, always use enumerate. It shows operational maturity.  nums = [10, 20, 30] for i, num in enumerate(nums): print(f&amp;quot;Ind...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
= High-Yield Python Patterns for Technical Interviews =&lt;br /&gt;
&lt;br /&gt;
== 1. Array (List) Iteration ==&lt;br /&gt;
Forget standard while loops or range(len(arr)) unless you specifically only need the index. Python has built-in ways to make this much cleaner and less prone to off-by-one errors.&lt;br /&gt;
&lt;br /&gt;
=== The Enumerate Pattern (Crucial) ===&lt;br /&gt;
Whenever you need both the index and the value, always use enumerate. It shows operational maturity.&lt;br /&gt;
&lt;br /&gt;
nums = [10, 20, 30]&lt;br /&gt;
for i, num in enumerate(nums):&lt;br /&gt;
print(f&amp;quot;Index: {i}, Value: {num}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== List Comprehensions ===&lt;br /&gt;
Use these for filtering or transforming data quickly. It saves space and looks highly professional.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Create a list of squares for even numbers only&lt;br /&gt;
&lt;br /&gt;
nums = [1, 2, 3, 4, 5]&lt;br /&gt;
evens_squared = [x2 for x in nums if x % 2 == 0]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Two-Pointer Setup ===&lt;br /&gt;
Very common for array manipulation and palindrome checks.&lt;br /&gt;
&lt;br /&gt;
left, right = 0, len(nums) - 1&lt;br /&gt;
while left &amp;lt; right:&lt;br /&gt;
# Do logic, then converge&lt;br /&gt;
left += 1&lt;br /&gt;
right -= 1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 2. String Manipulation ==&lt;br /&gt;
Remember: &amp;#039;&amp;#039;&amp;#039;Strings in Python are immutable.&amp;#039;&amp;#039;&amp;#039; You cannot do s[0] = &amp;#039;a&amp;#039;. You must build a new string or convert it to a list of characters first.&lt;br /&gt;
&lt;br /&gt;
=== Splitting and Joining ===&lt;br /&gt;
This is bread-and-butter for parsing messy log files or URIs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Splitting a messy log line&lt;br /&gt;
&lt;br /&gt;
log = &amp;quot;ERROR   User login failed&amp;quot;&lt;br /&gt;
parts = log.split() # Splits by any whitespace, stripping extras&lt;br /&gt;
&lt;br /&gt;
# Rebuilding a string from a list&lt;br /&gt;
&lt;br /&gt;
chars = [&amp;#039;G&amp;#039;, &amp;#039;o&amp;#039;, &amp;#039;o&amp;#039;, &amp;#039;g&amp;#039;, &amp;#039;l&amp;#039;, &amp;#039;e&amp;#039;]&lt;br /&gt;
word = &amp;quot;&amp;quot;.join(chars)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Reversing a String ===&lt;br /&gt;
Do not write a loop to reverse a string unless explicitly asked to. Use slicing.&lt;br /&gt;
&lt;br /&gt;
s = &amp;quot;security&amp;quot;&lt;br /&gt;
reversed_s = s[::-1] # &amp;quot;ytiruces&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Character Math (ASCII) ===&lt;br /&gt;
Crucial for encryption/decryption basics or mapping characters to an array index.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Convert char to ASCII int, and vice versa&lt;br /&gt;
&lt;br /&gt;
val = ord(&amp;#039;a&amp;#039;) # 97&lt;br /&gt;
char = chr(97) # &amp;#039;a&amp;#039;&lt;br /&gt;
&lt;br /&gt;
# Map &amp;#039;a&amp;#039;-&amp;#039;z&amp;#039; to 0-25&lt;br /&gt;
&lt;br /&gt;
index = ord(&amp;#039;c&amp;#039;) - ord(&amp;#039;a&amp;#039;) # 2&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 3. Linked Lists ==&lt;br /&gt;
You will almost always have to write the boilerplate class yourself if the platform doesn&amp;#039;t provide it. Memorize this exact structure.&lt;br /&gt;
&lt;br /&gt;
=== The Node Class ===&lt;br /&gt;
&lt;br /&gt;
class ListNode:&lt;br /&gt;
def **init**(self, val=0, next=None):&lt;br /&gt;
self.val = val&lt;br /&gt;
self.next = next&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== The &amp;quot;Dummy Node&amp;quot; Pattern ===&lt;br /&gt;
When doing Linked List insertions or deletions, always use a dummy node pointing to the head. It eliminates 90% of the edge cases regarding empty lists or modifying the very first node.&lt;br /&gt;
&lt;br /&gt;
def process_list(head):&lt;br /&gt;
dummy = ListNode(0)&lt;br /&gt;
dummy.next = head&lt;br /&gt;
curr = dummy&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
while curr.next:&lt;br /&gt;
    if curr.next.val == &amp;quot;target&amp;quot;:&lt;br /&gt;
        # Skip the node (Deletion)&lt;br /&gt;
        curr.next = curr.next.next&lt;br /&gt;
    else:&lt;br /&gt;
        curr = curr.next&lt;br /&gt;
        &lt;br /&gt;
return dummy.next # Returns the real, potentially modified head&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
== 4. The &amp;quot;No-IDE&amp;quot; Cheat Codes (Collections) ==&lt;br /&gt;
Google interviewers expect you to use Python&amp;#039;s standard library. Import these at the top of the file; it prevents you from reinventing the wheel.&lt;br /&gt;
&lt;br /&gt;
=== Counters (Frequency Maps) ===&lt;br /&gt;
&lt;br /&gt;
from collections import Counter&lt;br /&gt;
&lt;br /&gt;
# Instantly counts occurrences&lt;br /&gt;
&lt;br /&gt;
freq = Counter([&amp;#039;apple&amp;#039;, &amp;#039;apple&amp;#039;, &amp;#039;orange&amp;#039;])&lt;br /&gt;
&lt;br /&gt;
# freq = {&amp;#039;apple&amp;#039;: 2, &amp;#039;orange&amp;#039;: 1}&lt;br /&gt;
&lt;br /&gt;
# Get top N most frequent items (great for finding IP log spammers)&lt;br /&gt;
&lt;br /&gt;
top_two = freq.most_common(2)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== DefaultDict ===&lt;br /&gt;
Saves you from writing if key not in my_dict: my_dict[key] = [] a hundred times.&lt;br /&gt;
&lt;br /&gt;
from collections import defaultdict&lt;br /&gt;
&lt;br /&gt;
# Automatically initializes missing keys with an empty list&lt;br /&gt;
&lt;br /&gt;
adj_list = defaultdict(list)&lt;br /&gt;
adj_list[&amp;#039;node_A&amp;#039;].append(&amp;#039;node_B&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Deque (Double-Ended Queue) ===&lt;br /&gt;
If you need to pop from the left side of a list (queue.pop(0)), it takes $O(n)$ time and is a major red flag. Use a deque for $O(1)$ appends and pops on both ends.&lt;br /&gt;
&lt;br /&gt;
from collections import deque&lt;br /&gt;
queue = deque([1, 2, 3])&lt;br /&gt;
queue.append(4)      # Adds to right&lt;br /&gt;
queue.popleft()      # Removes from left in O(1) time&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 5. Sorting and Lambdas (NEW) ==&lt;br /&gt;
In security engineering loops, you are frequently asked to sort complex data structures (e.g., sorting logs by timestamp, or users by privilege level).&lt;br /&gt;
&lt;br /&gt;
=== Custom Sort Keys ===&lt;br /&gt;
Use lambda functions to sort tuples or dictionaries easily.&lt;br /&gt;
&lt;br /&gt;
logs = [(&amp;quot;10.0.0.1&amp;quot;, 500), (&amp;quot;192.168.1.1&amp;quot;, 200), (&amp;quot;10.0.0.2&amp;quot;, 403)]&lt;br /&gt;
&lt;br /&gt;
# Sort by the second item in the tuple (the HTTP status code)&lt;br /&gt;
&lt;br /&gt;
logs.sort(key=lambda x: x[1])&lt;br /&gt;
&lt;br /&gt;
# Sort in reverse (descending)&lt;br /&gt;
&lt;br /&gt;
logs.sort(key=lambda x: x[1], reverse=True)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 6. Graph &amp;amp; Tree Representation (NEW) ==&lt;br /&gt;
Security architecture heavily relies on trees (directory structures, IAM permissions) and graphs (network topologies, lateral movement).&lt;br /&gt;
&lt;br /&gt;
=== Adjacency List for Graphs ===&lt;br /&gt;
The standard way to represent a network. Combine this with the deque for a flawless Breadth-First Search (BFS).&lt;br /&gt;
&lt;br /&gt;
from collections import defaultdict, deque&lt;br /&gt;
&lt;br /&gt;
# Build the graph&lt;br /&gt;
&lt;br /&gt;
edges = [(&amp;quot;A&amp;quot;, &amp;quot;B&amp;quot;), (&amp;quot;A&amp;quot;, &amp;quot;C&amp;quot;), (&amp;quot;B&amp;quot;, &amp;quot;D&amp;quot;)]&lt;br /&gt;
graph = defaultdict(list)&lt;br /&gt;
for u, v in edges:&lt;br /&gt;
graph[u].append(v)&lt;br /&gt;
graph[v].append(u) # Omit this line if the graph is directed&lt;br /&gt;
&lt;br /&gt;
# Standard BFS Traversal&lt;br /&gt;
&lt;br /&gt;
def bfs(start_node):&lt;br /&gt;
visited = set([start_node])&lt;br /&gt;
queue = deque([start_node])&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
while queue:&lt;br /&gt;
    node = queue.popleft()&lt;br /&gt;
    print(f&amp;quot;Processing {node}&amp;quot;)&lt;br /&gt;
    &lt;br /&gt;
    for neighbor in graph[node]:&lt;br /&gt;
        if neighbor not in visited:&lt;br /&gt;
            visited.add(neighbor)&lt;br /&gt;
            queue.append(neighbor)&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
== 7. Critical Python Gotchas (NEW) ==&lt;br /&gt;
Avoid these traps on a whiteboard; interviewers specifically watch for them.&lt;br /&gt;
&lt;br /&gt;
=== The Mutable Default Argument Trap ===&lt;br /&gt;
Never use a list or dictionary as a default argument in a function. It evaluates once at definition time, meaning subsequent calls will share the same list memory.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# BAD:&lt;br /&gt;
&lt;br /&gt;
def add_to_list(val, my_list=[]):&lt;br /&gt;
my_list.append(val)&lt;br /&gt;
return my_list&lt;br /&gt;
&lt;br /&gt;
# GOOD:&lt;br /&gt;
&lt;br /&gt;
def add_to_list(val, my_list=None):&lt;br /&gt;
if my_list is None:&lt;br /&gt;
my_list = []&lt;br /&gt;
my_list.append(val)&lt;br /&gt;
return my_list&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Initializing Min/Max Values ===&lt;br /&gt;
When you need to find a minimum or maximum value by iterating, do not initialize your tracker to 0 or 999999. Use Python&amp;#039;s built-in infinity.&lt;br /&gt;
&lt;br /&gt;
max_val = float(&amp;#039;-inf&amp;#039;)&lt;br /&gt;
min_val = float(&amp;#039;inf&amp;#039;)&lt;/div&gt;</summary>
		<author><name>Mflavell</name></author>
	</entry>
</feed>