tgoop.com/DataScience4/8657
Create:
Last Update:
Last Update:
💡 Top 90 Python Coding Interview Questions (Set 2)
(Note: These are distinct from the first set, covering more variations, design questions, and advanced topics. Code snippets are conceptual.)
I. Arrays & Matrices
• 3Sum: Find all unique triplets that sum to zero.
# Sort the array, then use a two-pointer approach for each element.
• Rotate Image: Rotate an NxN matrix by 90 degrees in-place.
# Transpose the matrix, then reverse each row.
• Set Matrix Zeroes: If an element is 0, set its entire row and column to 0.
# Use the first row/col as markers to avoid extra space.
• Spiral Matrix: Traverse a matrix in a spiral order.
# Use four pointers for boundaries (top, bottom, left, right) and shrink them inward.
• Sort Colors (Dutch National Flag problem): Sort an array of 0s, 1s, and 2s.
# Use three pointers (low, mid, high) to partition in a single pass.
• Find Peak Element: Find an element that is greater than its neighbors.
# Use binary search. Compare mid with its right neighbor to decide which way to go.
• Maximum Product Subarray: Find the max product of a contiguous subarray.
# Track current max and min product because two negatives make a positive.
• Longest Consecutive Sequence: Find the length of the longest run of consecutive numbers.
# Use a hash set for O(1) lookups. Only start counting from the beginning of a sequence.
• Insert Interval: Insert a new interval into a list of non-overlapping intervals.
# Iterate through intervals, add non-overlapping ones, merge with overlapping ones.
• Merge Intervals: Merge all overlapping intervals.
# Sort intervals by start time, then iterate and merge.
• Jump Game: Can you reach the last index?
# Greedy approach. Keep track of the maximum reachable index.
• Next Permutation: Find the next lexicographically greater permutation.
# Find the first decreasing element from the right, swap it with the next largest, then reverse the rest.
• Find the Duplicate Number: Array
n+1 integers from 1 to n. Find the one duplicate.# Use Floyd's Tortoise and Hare (cycle detection) algorithm.
• Word Search: Find a word in a 2D grid of characters.
# Backtracking (DFS) from each cell. Mark visited cells to avoid reuse.
• Task Scheduler: Schedule tasks with a cooling period.
# Greedy approach using a frequency map or a max-heap.
• Is Subsequence: Check if one string is a subsequence of another.
# Simple two-pointer approach.
• Dot Product of Two Sparse Vectors:
# Store non-zero elements in a hash map or list of pairs to optimize.
II. Linked Lists
• Remove Duplicates from Sorted List II: Delete all nodes that have duplicate numbers.
# Use a sentinel/dummy head and a predecessor pointer.
• Rotate List: Rotate the list to the right by k places.
# Find length, connect tail to head to make a cycle, then break at the new tail.
• Partition List: Partition around a value
x.# Use two pointers to build two separate lists (less than `x` and >= `x`), then link them.
• Swap Nodes in Pairs: Swap every two adjacent nodes.
# Use careful pointer manipulation with a dummy head.
• Odd Even Linked List: Group odd nodes followed by even nodes.
# Use two pointers, one for the odd list tail and one for the even list tail.
• Flatten a Multilevel Doubly Linked List:
# Use DFS. When a child is found, insert its flattened list between the current node and its next.
• LRU Cache: Design a Least Recently Used cache.
BY Code With Python
Share with your friend now:
tgoop.com/DataScience4/8657
