Second Largest Element in Array | Brute β Better β Optimal | Python DSA #2
If you watched Video #1, you'll remember the most common bug there was initializing largest = 0 β and how it silently broke the code for negative arrays. This one is sneakier. π "Find the second largest element in an array" sounds like a beginner problem. But every batch I've taught, the same thing happens β students write a solution that works for [3, 7, 2, 9, 4], feel confident, and then crash the moment the array becomes [10, 10, 5]. That's why I didn't want to jump straight to the optimal trick. Instead, this video walks through the problem the way a real interview actually goes: β Brute force first β sort the array and pick the second-last. It works, but it's O(n log n), and we'll see why interviewers don't love it. β Better approach β two passes. Find the largest, then find the largest element that isn't equal to it. Handles duplicates cleanly. β Optimal β single pass, O(n), the version that wins points in the interview room. Each step builds on the previous one. If you follow the reasoning, you won't need to memorize this problem β you'll just know how to think about it. That's the goal of the whole series. One small ask: please code along, don't just watch. DSA is something your fingers have to learn, not just your eyes. β±οΈ Chapters: 0:00 The problem (and why it's trickier than it looks) 0:45 Brute force β sort and pick the second-last 2:30 Why brute force fails for [10, 10, 5] 3:15 Better approach β two-pass solution 5:40 The hidden bug 80% of students miss 7:00 Optimal single-pass O(n) solution 9:20 Dry run on a tricky test case 11:00 Time & space complexity comparison 12:00 Common interview follow-ups π Useful links: Full Python DSA playlist β [INSERT PLAYLIST LINK] Previous video β Largest Element in Array β [INSERT LINK] Source code on GitHub β [INSERT LINK] Practice problem (GFG / LeetCode) β [INSERT LINK] If you're prepping for placements, we also have IIT/IIIT senior interview stories on the channel β real people who cracked Google, Microsoft, Amazon, and yes, even Oracle (back when offers weren't getting revoked π ). Real prep, real conversations, no fluff. Drop a comment if this clicked for you. And honestly β drop one if it didn't. I read every single comment, and your doubts directly shape the next videos. Subscribe to WeGrad β https://www.youtube.com/@WeGradschool Visit us β https://www.wegrad.in Reach out β +91 93103 39438 See you in Video #3 β same time, same series. Until then, keep coding. π β Mohan Co-founder, WeGrad #PythonDSA #DSAinPython #SecondLargestInArray #CodingInterview #PlacementPreparation #ArrayProblems #WeGrad #PythonForBeginners
Download
0 formatsNo download links available.