Code With Python
39K subscribers
841 photos
24 videos
22 files
746 links
This channel delivers clear, practical content for developers, covering Python, Django, Data Structures, Algorithms, and DSA – perfect for learning, coding, and mastering key programming skills.
Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
Topic: 20 Important Python OpenCV Interview Questions with Brief Answers

---

1. What is OpenCV?
OpenCV is an open-source library for computer vision, image processing, and machine learning.

2. How do you read and display an image using OpenCV?
Use cv2.imread() to read and cv2.imshow() to display images.

3. What color format does OpenCV use by default?
OpenCV uses BGR format instead of RGB.

4. How to convert an image from BGR to Grayscale?
Use cv2.cvtColor(image, cv2.COLOR_BGR2GRAY).

5. What is the difference between `cv2.imshow()` and matplotlib’s `imshow()`?
cv2.imshow() uses BGR and OpenCV windows; matplotlib uses RGB and inline plotting.

6. How do you write/save an image to disk?
Use cv2.imwrite('filename.jpg', image).

7. What are image thresholds?
Techniques to segment images into binary images based on pixel intensity.

8. What is Gaussian Blur, and why is it used?
A smoothing filter to reduce image noise and detail.

9. Explain the Canny Edge Detection process.
It detects edges using gradients, non-maximum suppression, and hysteresis thresholding.

10. How do you capture video from a webcam using OpenCV?
Use cv2.VideoCapture(0) and read frames in a loop.

11. What are contours in OpenCV?
Curves joining continuous points with the same intensity, used for shape detection.

12. How do you find and draw contours?
Use cv2.findContours() and cv2.drawContours().

13. What are morphological operations?
Operations like erosion and dilation that process shapes in binary images.

14. What is the difference between erosion and dilation?
Erosion removes pixels on object edges; dilation adds pixels.

15. How to perform image masking in OpenCV?
Use cv2.bitwise_and() with an image and a mask.

16. What is the use of `cv2.waitKey()`?
Waits for a key event for a specified time; needed to display images properly.

17. How do you resize an image?
Use cv2.resize(image, (width, height)).

18. Explain how to save a video using OpenCV.
Use cv2.VideoWriter() with codec, fps, and frame size.

19. How to convert an image from OpenCV BGR format to RGB for matplotlib?
Use cv2.cvtColor(image, cv2.COLOR_BGR2RGB).

20. What is the difference between `cv2.threshold()` and `cv2.adaptiveThreshold()`?
cv2.threshold() applies global threshold; adaptiveThreshold() applies varying thresholds locally.

---

Summary

These questions cover basic to intermediate OpenCV concepts essential for interviews and practical use.

---

#Python #OpenCV #InterviewQuestions #ComputerVision #ImageProcessing

https://t.me/DataScience4
3
Topic: Data Structures – Trees – Top 15 Interview Questions with Answers

---

### 1. What is a tree data structure?

A hierarchical structure with nodes connected by edges, having a root node and child nodes with no cycles.

---

### 2. What is the difference between binary tree and binary search tree (BST)?

A binary tree allows up to two children per node; BST maintains order where left child < node < right child.

---

### 3. What are the types of binary trees?

Full, perfect, complete, skewed (left/right), and balanced binary trees.

---

### 4. Explain tree traversal methods.

Inorder (LNR), Preorder (NLR), Postorder (LRN), and Level Order (BFS).

---

### 5. What is a balanced tree? Why is it important?

A tree where the height difference between left and right subtrees is minimal to ensure O(log n) operations.

---

### 6. What is an AVL tree?

A self-balancing BST maintaining balance factor (-1, 0, 1) with rotations to balance after insert/delete.

---

### 7. What are rotations in AVL trees?

Operations (Left, Right, Left-Right, Right-Left) used to rebalance the tree after insertion or deletion.

---

### 8. What is a Red-Black Tree?

A balanced BST with red/black nodes ensuring balance via color rules, offering O(log n) operations.

---

### 9. How does a Trie work?

A tree structure used for storing strings, where nodes represent characters, allowing fast prefix searches.

---

### 10. What is the height of a binary tree?

The number of edges on the longest path from root to a leaf node.

---

### 11. How do you find the lowest common ancestor (LCA) of two nodes?

By traversing from root, checking if nodes lie in different subtrees, or by storing parent pointers.

---

### 12. What is the difference between DFS and BFS on trees?

DFS explores as far as possible along branches; BFS explores neighbors level by level.

---

### 13. How do you detect if a binary tree is a BST?

Check if inorder traversal yields a sorted sequence or verify node values within valid ranges recursively.

---

### 14. What are leaf nodes?

Nodes with no children.

---

### 15. How do you calculate the number of nodes in a complete binary tree?

Using the formula: number\_of\_nodes = 2^(height + 1) - 1 (if perfect), else traverse and count.

---

### Exercise

Write functions for inorder, preorder, postorder traversals, check if tree is BST, and find LCA of two nodes.

---

#DSA #Trees #InterviewQuestions #BinaryTrees #Python #Algorithms

https://t.me/DataScience4
2
Top 100 Python Interview Questions & Answers

#Python #InterviewQuestions #CodingInterview #Programming #PythonDeveloper

👇👇👇👇
Please open Telegram to view this post
VIEW IN TELEGRAM
1
Top 100 Python Interview Questions & Answers

#Python #InterviewQuestions #CodingInterview #Programming #PythonDeveloper

Part 1: Core Python Fundamentals (Q1-20)

#1. Is Python a compiled or an interpreted language?
A: Python is an interpreted language. The Python interpreter reads and executes the source code line by line, without requiring a separate compilation step. This makes development faster but can result in slower execution compared to compiled languages like C++.

#2. What is the GIL (Global Interpreter Lock)?
A: The GIL is a mutex (a lock) that allows only one thread to execute Python bytecode at a time within a single process. This means even on a multi-core processor, a single Python process cannot run threads in parallel. It simplifies memory management but is a performance bottleneck for CPU-bound multithreaded programs.

#3. What is the difference between Python 2 and Python 3?
A: Key differences include:
print: In Python 2, print is a statement (print "hello"). In Python 3, it's a function (print("hello")).
Integer Division: In Python 2, 5 / 2 results in 2 (floor division). In Python 3, it results in 2.5 (true division).
Unicode: In Python 3, strings are Unicode (UTF-8) by default. In Python 2, you had to explicitly use u"unicode string".

#4. What are mutable and immutable data types in Python?
A:
Mutable: Objects whose state or contents can be changed after creation. Examples: list, dict, set.
Immutable: Objects whose state cannot be changed after creation. Examples: int, float, str, tuple, frozenset.

# Mutable example
my_list = [1, 2, 3]
my_list[0] = 99
print(my_list)

[99, 2, 3]


#5. How is memory managed in Python?
A: Python uses a private heap to manage memory. A built-in garbage collector automatically reclaims memory from objects that are no longer in use. The primary mechanism is reference counting, where each object tracks the number of references to it. When the count drops to zero, the object is deallocated.

#6. What is the difference between is and ==?
A:
== (Equality): Checks if the values of two operands are equal.
is (Identity): Checks if two variables point to the exact same object in memory.

list_a = [1, 2, 3]
list_b = [1, 2, 3]
list_c = list_a

print(list_a == list_b) # True, values are the same
print(list_a is list_b) # False, different objects in memory
print(list_a is list_c) # True, same object in memory

True
False
True


#7. What is PEP 8?
A: PEP 8 (Python Enhancement Proposal 8) is the official style guide for Python code. It provides conventions for writing readable and consistent Python code, covering aspects like naming conventions, code layout, and comments.

#8. What is the difference between a .py and a .pyc file?
A:
.py: This is the source code file you write.
.pyc: This is the compiled bytecode. When you run a Python script, the interpreter compiles it into bytecode (a lower-level, platform-independent representation) and saves it as a .pyc file to speed up subsequent executions.

#9. What are namespaces in Python?
A: A namespace is a system that ensures all names in a program are unique and can be used without conflict. It's a mapping from names to objects. Python has different namespaces: built-in, global, and local.