Tuesday, 30 June 2026

Python Coding challenge - Day 1194| What is the output of the following Python Code?

Code 




import heapq nums = [5, 8, 2, 9] heapq.heapify(nums) heapq.heappush(nums, 1) print(heapq.heappop(nums)) 



print(heapq.heappop(nums))

Explanation:

๐Ÿ”น 1. Import the heapq Module
import heapq
✅ Explanation
Python imports the heapq module.
It provides functions to create and manage a Min Heap.
A Min Heap always keeps the smallest value at the top, making it easy to retrieve.

Think of it like a priority queue in a hospital.

Priority Queue

Emergency (1)   ← Served First
Normal (2)
General (5)
Regular (9)

The smallest priority number is always served first.

๐Ÿ”น 2. Create a Normal List
nums = [5, 8, 2, 9]
✅ Explanation

Initially, this is just a normal list.

Index

0   1   2   3
│   │   │   │
5   8   2   9

Python has no idea that this list should behave like a heap.

๐Ÿ”น 3. Convert List into a Min Heap
heapq.heapify(nums)
✅ Explanation

heapify() doesn't sort the list completely.

Instead, it rearranges elements so that every parent is smaller than its children.

Before:

[5, 8, 2, 9]

After:

[2, 8, 5, 9]

Tree representation:

        2
      /   \
     8     5
    /
   9

Notice something interesting:

8 > 5

Yet this is still a valid heap.

Why?

Because the heap only checks the relationship between a parent and its children, not between sibling nodes.

๐Ÿ”น 4. Insert a New Value
heapq.heappush(nums, 1)
✅ Explanation

Python first inserts 1 at the end.

Temporary heap:

[2, 8, 5, 9, 1]

Now the heap rule is broken because:

1 < 8

So Python starts moving 1 upward.

Step 1:

        2
      /   \
     8     5
    / \
   9   1

Swap with parent (8):

        2
      /   \
     1     5
    / \
   9   8

Still,

1 < 2

Swap again:

        1
      /   \
     2     5
    / \
   9   8

Final heap:

[1, 2, 5, 9, 8]

This upward movement is called Heapify Up (or Bubble Up).


๐Ÿ”น 5. First heappop()
print(heapq.heappop(nums))
✅ Explanation

Python removes the root because it is the smallest.

Current heap:

        1
      /   \
     2     5
    / \
   9   8

Output:

1

But removing the root creates an empty space.

Python moves the last element (8) to the root.

Temporary heap:

        8
      /   \
     2     5
    /
   9

Heap rule is broken.

Since:

8 > 2

Swap:

        2
      /   \
     8     5
    /
   9

Heap property restored.

Current heap:

[2, 8, 5, 9]

๐Ÿ”น 6. Second heappop()
print(heapq.heappop(nums))
✅ Explanation

Again, Python removes the root.

Current heap:

        2
      /   \
     8     5
    /
   9

Output:

2

Move last element (9) to the top.

Temporary:

        9
      /   \
     8     5

Compare with children.

Smallest child is:

5

Swap:

        5
      /   \
     8     9

Heap restored.

Remaining heap:

[5, 8, 9]

๐ŸŽฏ Final Output
1
2

Book: Data Analysis Using ML Models (RandomForestClassifier, DecisionTreeClassifier, LogisticRegression)

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (295) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (30) Azure (11) BI (10) Books (262) Bootcamp (12) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (300) Cybersecurity (32) data (6) Data Analysis (38) Data Analytics (25) data management (16) Data Science (376) Data Strucures (22) Deep Learning (184) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (21) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (74) Git (12) Google (53) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (327) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (14) PHP (20) Projects (34) Python (1391) Python Coding Challenge (1176) Python Mathematics (1) Python Mistakes (51) Python Quiz (555) Python Tips (18) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (20) SQL (52) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)