Загрузка...

Find 2nd Largest Number in Python | Using Sort & Without Sort | Python Interview Question Explained

Welcome back to @ProgrammerChandra 👋

In this video, we’ll learn how to find the second largest number in a list using Python — a popular Python interview question!

We’ll explore two methods step by step 👇
✅ Method 1: Using sort() function
✅ Method 2: Without using sort — logic-based approach

This question is commonly asked in coding interviews to test your understanding of loops, conditions, and comparisons in Python.
By the end of this video, you’ll understand both methods clearly and be ready to tackle similar interview problems confidently.
# Method 1: Using sort()
nums = [10, 5, 8, 20, 15]
nums.sort()
print("Second Largest:", nums[-2])

# Method 2: Without using sort()
nums = [10, 5, 8, 20, 15]
first = second = float('-inf')
for n in nums:
if n greaterthan first:
second, first = first, n
elif n greaterthan second and n != first:
second = n
print("Second Largest:", second)

#pythoninterview #listcomprehension #pythonexamples #programmerchandra #findthelongestword #python #removeduplicate #string #pythontricks #fullstack #pythontips #pythonbeginners #webdev #shorts #codewithpython #beginnerpython #pythonforbeginners #youtubeshorts #coding #pythonprojects #opensource #programmingshorts #codewithme #programmingtutorial #beginnerfriendly #creativecoding #learntocode #codingshorts #learnpython #tutorial #pythonshorts #programming #python3 #codeart #developers #pythonsyntax #cleanCode #developer #codingtricks #pythontutorial #pythoncoding

Видео Find 2nd Largest Number in Python | Using Sort & Without Sort | Python Interview Question Explained канала ProgrammerChandra
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять