Skip to content

Latest commit

 

History

History
277 lines (199 loc) · 6.38 KB

35 Python interview questions for experienced.md

File metadata and controls

277 lines (199 loc) · 6.38 KB

Python Coding Interview Questions And Answers - Experienced

Here Coding compiler sharing a list of 35 Python interview questions for experienced. These Python questions are prepared by expert Python developers. This list of interview questions on Python will help you to crack your next Python job interview. All the best for your future and happy python learning

Hits

1. Debugging a Python Program

# By using this command we can debug a python program
python -m pdb python-script.py

2. Yield Keyword in Python

The keyword in Python can turn any function into a generator. Yields work like a standard return keyword.

def days(index):
    day = ['S','M','T','W','Tr','F','St']            
    yield day[index]    
    yield day[index+1]  
  
res = days(0)
print(next(res), next(res))

> S M

3. Converting a List into a String

When we want to convert a list into a string, we can use the <.join()> method which joins all the elements into one and returns as a string.

days = ['S','M','T','W','Tr','F','St'] 
ltos = ' '.join(days)
print(ltos)

> S M T W Tr F St

4. Converting a List into a Tuple

By using Python <tuple()> function we can convert a list into a tuple. But we can’t change the list after turning it into tuple, because it becomes immutable.

days = ['S','M','T','W','Tr','F','St'] 
ltos = tuple(days)
print(ltos)

> ('S', 'M', 'T', 'W', 'Tr', 'F', 'St')

5. Converting a List into a Set

User can convert list into set by using <set()> function.

days = ['S','M','T','W','Tr','F','St'] 
ltos = set(days)
print(ltos)

> {'T', 'W', 'M', 'F', 'S', 'Tr', 'St'}

6. Counting the occurrences of a particular element in the list

We can count the occurrences of an individual element by using a <count()> function.

days = ['S','M','W', 'M','M','F','S']

print(days.count('M'))

> 3

6.1. Counting the occurrences of elements in the list

days = ['S','M','M','M','F','S']
y = set(days)

print([[x,days.count(x)] for x in y])

> [['M', 3], ['S', 2], ['F', 1]]

7. Creating a NumPy Array in Python

NumPy arrays are more flexible then lists in Python.

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(arr)
print(type(arr))

> [1 2 3 4 5]
  <class 'numpy.ndarray'>
  

8. Implement a LRU (Least Recently Used) Cache

from collections import OrderedDict

class LRUCache:
    def __init__(self, capacity: int):
        self.cache = OrderedDict()
        self.capacity = capacity

    def get(self, key: int) -> int:
        if key not in self.cache:
            return -1
        else:
            self.cache.move_to_end(key)
            return self.cache[key]

    def put(self, key: int, value: int) -> None:
        if key in self.cache:
            self.cache.move_to_end(key)
        self.cache[key] = value
        if len(self.cache) > self.capacity:
            self.cache.popitem(last=False)

# Example usage:
lru = LRUCache(2)
lru.put(1, 1)
lru.put(2, 2)
print(lru.get(1)) 
lru.put(3, 3)

> 1

9. Find the Longest Substring Without Repeating Characters

def length_of_longest_substring(s: str) -> int:
    char_index_map = {}
    start = max_length = 0

    for i, char in enumerate(s):
        if char in char_index_map and char_index_map[char] >= start:
            start = char_index_map[char] + 1
        char_index_map[char] = i
        max_length = max(max_length, i - start + 1)

    return max_length

# Example usage:
print(length_of_longest_substring("abcabcbb"))

> 3

10. Find the Kth Largest Element in an Array

import heapq

def find_kth_largest(nums: list, k: int) -> int:
    return heapq.nlargest(k, nums)[-1]

# Example usage:
print(find_kth_largest([3, 2, 1, 5, 6, 4], 2))

> 5

11. Detect a Cycle in a Linked List

class ListNode:
    def __init__(self, value=0, next=None):
        self.value = value
        self.next = next

def has_cycle(head: ListNode) -> bool:
    slow = fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow == fast:
            return True
    return False

# Example usage:
# Creating a linked list with a cycle for demonstration:
node1 = ListNode(1)
node2 = ListNode(2)
node3 = ListNode(3)
node1.next = node2
node2.next = node3
node3.next = node1  # Cycle here
print(has_cycle(node1))

> True

12. Serialize and Deserialize a Binary Tree

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Codec:
    def serialize(self, root: TreeNode) -> str:
        def dfs(node):
            if not node:
                result.append("None")
            else:
                result.append(str(node.val))
                dfs(node.left)
                dfs(node.right)
        result = []
        dfs(root)
        return ','.join(result)

    def deserialize(self, data: str) -> TreeNode:
        def dfs():
            val = next(values)
            if val == "None":
                return None
            node = TreeNode(int(val))
            node.left = dfs()
            node.right = dfs()
            return node
        values = iter(data.split(','))
        return dfs()

# Example usage:
codec = Codec()
tree = TreeNode(1, TreeNode(2), TreeNode(3, TreeNode(4), TreeNode(5)))
serialized = codec.serialize(tree)
print(serialized) 
deserialized = codec.deserialize(serialized)

> "1,2,None,None,3,4,None,None,5,None,None"



Open Source Maintened by - Tanu Nanda Prabhu made-with-Markdown