TGViewer
Coding Interview Resources Coding Interview Resources @crackingthecodinginterview ยท 52.2K subscribers
Post #3191 1.09K
โœ… 10 Useful Python Interview Code Snippets ๐Ÿ๐Ÿ’ผ

1. Reverse a string:

s = "hello"
print(s[::-1]) # Output: 'olleh'


2. Check for a palindrome:

def is_palindrome(s):
return s == s[::-1]


3. Count word frequency in a list:

from collections import Counter
words = ['apple', 'banana', 'apple']
print(Counter(words))


4. Swap two variables:

a, b = 5, 10
a, b = b, a


5. Fibonacci using recursion:

def fib(n):
return n if n <= 1 else fib(n-1) + fib(n-2)


6. Find duplicate elements:

lst = [1,2,3,2,4]
duplicates = set([x for x in lst if lst.count(x) > 1])


7. Check if list is sorted:

def is_sorted(lst):
return lst == sorted(lst)


8. Flatten a 2D list:

matrix = [[1, 2], [3, 4]]
flat = [num for row in matrix for num in row]


9. Read a file line by line:

with open('file.txt') as f:
for line in f:
print(line.strip())


10. Lambda & Map usage:

nums = [1, 2, 3]
squares = list(map(lambda x: x**2, nums))


๐Ÿ’ก Practice these with variations, especially for lists, strings, and dictionaries.

๐Ÿ’ฌ Tap โค๏ธ for more!
  • โค 1
More from @crackingthecodinginterview
  1. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  2. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  3. Sep 29, 2026โœ… Daily Coding Habits That Make You a Better Developer ๐Ÿง ๐Ÿ’ปโœจ 1๏ธโƒฃ Code Every Day (Even 30 Mโ€ฆ
  4. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  5. Sep 28, 2026Hereโ€™s a DSA problem-solving cheat sheet that will help you solve 90โ€“95% of questions thatโ€ฆ
  6. Sep 28, 2026๐ŸŽ“ ๐—›๐—”๐—ฅ๐—ฉ๐—”๐—ฅ๐—— ๐—จ๐—ก๐—œ๐—ฉ๐—˜๐—ฅ๐—ฆ๐—œ๐—ง๐—ฌ ๐—™๐—ฅ๐—˜๐—˜ ๐—ข๐—ก๐—Ÿ๐—œ๐—ก๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐Ÿ˜ Dreaming ofโ€ฆ
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 โ†’