← Back to Python Course | Chapter 14: Advanced Python & Tools | Lesson 5 of 15

Python Interview प्रश्न

Interview प्रश्न वे classic puzzles हैं जो लोगों से programming jobs के लिए apply करते समय पूछे जाते हैं। इनका अभ्यास करना किसी बड़े मैच से पहले रिहर्सल करने जैसा है।

Collections को Reverse करना

किसी string या list को reverse करना लगभग हर जगह पूछा जाने वाला warm-up interview सवाल है, और Python का slice notation इसे बेहद आसान बना देता है: s[::-1] किसी भी sequence को एक expression में, पीछे की ओर step करते हुए reverse कर देता है, बिना किसी explicit loop या अलग reversal function की ज़रूरत के।

उदाहरण: Reversing Collections

python
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

python
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

python
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()

python
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

python
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]))
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.