TGViewer
Learn Python Coding Learn Python Coding @pythonre · 40.9K subscribers
Post #8659 651
# Greedy. Add profit `prices[i] - prices[i-1]` whenever it's positive.

• Coin Change 2: Number of combinations that make up an amount.
# Unbounded knapsack DP. `dp[i]` is the number of ways to make amount `i`.

• Minimum Path Sum: Find the min path sum from top-left to bottom-right in a grid.
# 2D DP. `dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1])`.

• House Robber II: Houses are in a circle.
# Run the standard House Robber algorithm twice: once excluding the first house, once excluding the last.

• Palindromic Substrings: Count how many substrings are palindromic.
# Expand from center for every possible center (2n-1 centers).

• Longest Palindromic Subsequence:
# 2D DP. `dp[i][j]` = length of LPS in `s[i:j+1]`.

• Target Sum: Find ways to assign + or - to numbers to reach a target.
# DP or backtracking with memoization.

• Letter Combinations of a Phone Number:
# Classic backtracking problem.

• Knight's Shortest Path on a Chessboard:
# Use Breadth-First Search (BFS).

• Generate All Subsequences of a String:
# Backtracking: at each character, either include it or don't.

• Interleaving String:
# 2D DP. `dp[i][j]` is true if `s1[:i]` and `s2[:j]` can form `s3[:i+j]`.

• Paint House:
# Simple 1D DP. Track the min cost to paint the current house each of the three colors.


VI. Sorting, Searching & Heaps

• K Closest Points to Origin:
# Use a max-heap of size k or `heapq.nsmallest`.

• Find Kth Largest Element in a Stream:
# Maintain a min-heap of size k.

• Median of Two Sorted Arrays:
# Binary search on the smaller array to find the correct partition.

• Find Median from Data Stream:
# Use two heaps: a max-heap for the lower half and a min-heap for the upper half.

• Time Based Key-Value Store:
# Store values in a list sorted by timestamp for each key. Use binary search (`bisect_right`) for lookups.

• Reorganize String: Rearrange so no two adjacent characters are the same.
# Greedy approach with a max-heap or by counting character frequencies.

• Wiggle Sort II: Reorder so nums[0] < nums[1] > nums[2] < ....
# Find the median, then use a three-way partition and clever index mapping.

• Pancake Sorting:
# Find the max, flip it to the front, then flip it to its correct sorted position. Repeat.

• Custom Sort String:
# Use a hash map to count chars in the string, then build the result based on the custom order.


VII. Design Questions

• Design a Logger Rate Limiter:
# Use a hash map to store the last printed timestamp for each message.

• Design Tic-Tac-Toe:
# Store counts for rows, columns, and diagonals to check for a win in O(1).

• Design an In-Memory File System:
# Use a Trie-like structure where each node is a directory/file.

• Design a Hit Counter:
# Use a queue or deque to store timestamps of hits within the last 5 minutes.

• Design TinyURL:
# Map a long URL to a unique hash or incrementing ID, then convert to a base-62 string.

• Design a Web Crawler:
# Use BFS to explore links and a set to keep track of visited URLs.

• Design Twitter's Data Structures:
# Hash maps for users, tweets. A list/deque for a user's own tweets. A more complex structure for the news feed.

• Design Search Autocomplete System:
# Use a Trie (Prefix Tree) where nodes store search frequency.


VIII. Bit Manipulation & Math

• Power of Two:
  • ❤ 1
More from @pythonre
  1. Oct 17, 2026😰 15-Hour "Python for Data Science" Course ⚡️ Learn Python for Data Science https://enghu…
  2. Oct 13, 2026https://substack.com/@husseinsheikho?utm_source=share&utm_medium=android&r=3hwnr8 Follow m…
  3. Oct 10, 2026📚 Download 10 Python books for FREE: 1. Think Python — O’Reilly https://t.co/X7y3pX68IW 2…
  4. Oct 7, 2026Python Lists: A Quick Reference 🐍 * Definition: Lists are ordered, mutable collections th…
  5. Oct 7, 2026📱 270 tools for Python developers The repository contains: 🫡 Linters and formatters; 🫡…
  6. Oct 7, 2026Hands On Python Data Science - Data Science Bootcamp Master Python for Data Science with R…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →