Python Interview प्रश्न
In this page:
Collections को Reverse करना
किसी string या list को reverse करना लगभग हर जगह पूछा जाने वाला warm-up interview सवाल है, और Python का slice notation इसे बेहद आसान बना देता है: s[::-1] किसी भी sequence को एक expression में, पीछे की ओर step करते हुए reverse कर देता है, बिना किसी explicit loop या अलग reversal function की ज़रूरत के।
उदाहरण: Reversing Collections
text = "hello"
print(text[::-1])
Anagram और Palindrome जाँचना
Palindrome checks (क्या कोई string आगे और पीछे से एक जैसी पढ़ी जाती है?) और anagram checks (क्या दो strings में बिल्कुल वही letters हैं, बस फेरबदल किए हुए?) आम follow-up सवाल हैं जो यह test करते हैं कि आप सही built-in tool इस्तेमाल करते हैं या नहीं -- palindromes के लिए slicing, anagrams के लिए sorted() या Counter -- बजाय इसके कि character-counting logic खुद हाथ से लिखें।
उदाहरण: Anagram and Palindrome Verification
def is_palindrome(s):
return s == s[::-1] # reversed slice compared to the original
def is_anagram(a, b):
return sorted(a) == sorted(b) # same letters rearranged sort to the same sequence
print(is_palindrome("level"))
print(is_anagram("listen", "silent"))
Character Frequencies गिनना
किसी string में हर character कितनी बार आता है यह गिनना dictionary (या collections.Counter, जो कम code में वही काम करता है) से सबसे साफ तरीके से हल होता है, जो string से एक बार guzarte हुए हर character को एक running count से map करता है।
उदाहरण: Counting Character Frequencies
from collections import Counter
text = "banana"
print(Counter(text)) # counts how many times each character appears
List Comprehensions बनाम map()
Interviewers अक्सर आपसे list comprehensions और equivalent map()/filter() calls के बीच अनुवाद करने को कहते हैं, क्योंकि दोनों एक ही 'हर element को transform करो' या 'सिर्फ matching elements रखो' विचार को व्यक्त करते हैं -- दोनों में सहज होना यह दिखाता है कि आप underlying pattern समझते हैं, सिर्फ उसकी एक particular syntax नहीं।
उदाहरण: List Comprehensions vs map()
nums = [1, 2, 3]
squares_comp = [x * x for x in nums] # list comprehension
squares_map = list(map(lambda x: x * x, nums)) # equivalent using map()
print(squares_comp == squares_map)
Sets से Duplicates ढूँढना
किसी list में duplicates को efficiently ढूँढने का मतलब है naive O(n²) nested-loop comparison से बचना और इसकी जगह पहले से देखी गई values को track करने के लिए set इस्तेमाल करना, जिससे average O(n) time मिलता है क्योंकि set membership checks O(1) होते हैं -- यह थोड़ी extra memory के बदले बड़े speed improvement पाने का एक textbook उदाहरण है।
उदाहरण: Finding Duplicates with Sets
def find_duplicates(items):
seen = set()
duplicates = set()
for item in items:
if item in seen: # O(1) membership check instead of a nested loop
duplicates.add(item)
seen.add(item)
return duplicates
print(find_duplicates([1, 2, 3, 2, 4, 1]))
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- Python PEP 8 Style Guide
- Python Debugging Techniques
- Python Testing with unittest
- Python Common Mistakes
- Python Interview Questions
- Python map() & filter()
- Python reduce()
- Python zip() & enumerate()
- Python sorted() & key Functions
- Python Comprehensions Advanced
- Python Turtle Graphics
- Python tkinter Introduction
- Python tkinter Widgets
- Python pygame Introduction
- Python Mini Projects