Close Menu
Luminari | Learn Docker, Kubernetes, AI, Tech & Interview PrepLuminari | Learn Docker, Kubernetes, AI, Tech & Interview Prep
  • Home
  • Technology
    • Docker
    • Kubernetes
    • AI
    • Cybersecurity
    • Blockchain
    • Linux
    • Python
    • Tech Update
    • Interview Preparation
    • Internet
  • Entertainment
    • Movies
    • TV Shows
    • Anime
    • Cricket
What's Hot

Lost in Starlight Movie Review – Review

June 4, 2025

The World Ends with You Director Tatsuya Kando Leaves Square Enix – News

June 4, 2025

Windsurf says Anthropic is limiting its direct access to Claude AI models

June 4, 2025
Facebook X (Twitter) Instagram
Facebook X (Twitter) Instagram
Luminari | Learn Docker, Kubernetes, AI, Tech & Interview Prep
  • Home
  • Technology
    • Docker
    • Kubernetes
    • AI
    • Cybersecurity
    • Blockchain
    • Linux
    • Python
    • Tech Update
    • Interview Preparation
    • Internet
  • Entertainment
    • Movies
    • TV Shows
    • Anime
    • Cricket
Luminari | Learn Docker, Kubernetes, AI, Tech & Interview PrepLuminari | Learn Docker, Kubernetes, AI, Tech & Interview Prep
Home » Program to find second largest number optimized Copy
Misc

Program to find second largest number optimized Copy

HarishBy HarishJuly 14, 2024No Comments1 Min Read
Facebook Twitter Pinterest LinkedIn Reddit WhatsApp Email
Share
Facebook Twitter Pinterest Reddit WhatsApp Email
Question:

Second largest number complete solution

example : [22,3,4,5,6,11,3,33] output should be : 22

I know this is the common question asked in interview, there are number of solutions to this. But what about optimization?. This solution will give you optimized implementation

Logic:

  1. Initialize two variables, large and sec_large, to None.

  2. Check if the input list l1 has at least 2 elements (using len(l1) > 1).

  3. If the condition is met, iterate through each element i in the list:

    • If large is None or i is greater than large, update large and sec_large.

    • Otherwise, if sec_large is None or i is between sec_large and large, update sec_large.

  4. If the list has fewer than 2 elements, return the message “At least 2 numbers required.”

  5. Otherwise, return a tuple containing sec_large (the second largest element) and large (the largest element).

Solution in python:

At the time of interview i went with 2 for loop as i was prepared for this question. But interviewer asked me to improve this over and over finally he was satisfied with the below implementation

data = [10, 5, 20, 15, 30]

print(second_large(data)) # Output: (20, 30)

Preparing for interview?

  • Checkout our Interview prep page, It might help you.

Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Email
Previous ArticleInterview questions- SAN, FLOGI, PLOGI, Process login and zoning
Next Article Learn Docker for free : Building images
Harish
  • Website
  • X (Twitter)

Related Posts

We’ve entered an era of fintech maximalism according to Mark Goldberg

April 16, 2025

Apple says zero-day bugs exploited against ‘specific targeted individuals’ using iOS

April 16, 2025

India’s Uber-rival BluSmart appears to suspend service in wake of EV loan probe

April 16, 2025

Bluesky feed builder Graze raises $1M, rolls out ads

April 16, 2025

Kia is pitching Americans an affordable EV sedan with EV4 debut

April 16, 2025

NSO lawyer names Mexico, Saudi Arabia, and Uzbekistan as spyware customers accused of 2019 WhatsApp hacks

April 16, 2025
Add A Comment
Leave A Reply Cancel Reply

Our Picks

Lost in Starlight Movie Review – Review

June 4, 2025

The World Ends with You Director Tatsuya Kando Leaves Square Enix – News

June 4, 2025

Windsurf says Anthropic is limiting its direct access to Claude AI models

June 4, 2025

Sazae-san Anime Recasts Ikura, Kaori, Rika Roles – News

June 3, 2025
Don't Miss
Blockchain

Tokenized funds hit $5.7B, scaling fast — Moody’s

June 3, 20253 Mins Read

Tokenized short-term funds, a new class of digital financial products bridging traditional and decentralized finance,…

May’s top 8 projects on BNB Chain reflect sector-wide growth

June 3, 2025

Polygon grants lending protocol $300K as crosschain lending enters the spotlight

June 3, 2025

DeFi must return to its P2P origins.

June 3, 2025

Subscribe to Updates

Subscribe to our newsletter and never miss our latest news

Subscribe my Newsletter for New Posts & tips Let's stay updated!

About Us
About Us

Welcome to Luminari, your go-to hub for mastering modern tech and staying ahead in the digital world.

At Luminari, we’re passionate about breaking down complex technologies and delivering insights that matter. Whether you’re a developer, tech enthusiast, job seeker, or lifelong learner, our mission is to equip you with the tools and knowledge you need to thrive in today’s fast-moving tech landscape.

Our Picks

Windsurf says Anthropic is limiting its direct access to Claude AI models

June 4, 2025

Anthropic’s AI is writing its own blog — with human oversight

June 3, 2025

The OpenAI board drama is reportedly turning into a movie

June 3, 2025

Subscribe to Updates

Subscribe to our newsletter and never miss our latest news

Subscribe my Newsletter for New Posts & tips Let's stay updated!

Facebook X (Twitter) Instagram Pinterest
  • Home
  • About Us
  • Advertise With Us
  • Contact Us
  • DMCA Policy
  • Privacy Policy
  • Terms & Conditions
© 2025 luminari. Designed by luminari.

Type above and press Enter to search. Press Esc to cancel.