Day 34 of #100DaysOfCode: SearchRange challenge on LeetCode

🚀 Day 34 of #100DaysOfCode — LeetCode + HackerRank Edition! Today’s challenge was a subtle twist on binary search — finding the first and last position of a target in a sorted array. 🔗 Problem: searchRange(self, nums: List[int], target: int) (LeetCode) 📌 Challenge: Return the starting and ending index of a target value in O(log n) time. 🔍 Approach: → Used two binary searches — one to find the leftmost index, one for the rightmost → In findLeft(), moved left even after finding the target to ensure it's the first occurrence → In findRight(), moved right after finding the target to ensure it's the last occurrence → Carefully handled edge cases like empty arrays and single-element matches → Avoided brute-force scanning by sticking to binary search logic 💡 What made it click: → Realized that finding one match isn’t enough — we need both ends of the range → Fixed a key bug: I was using while low < high — switching to low <= high made it work → Learned that binary search isn’t just about finding a value — it can be adapted to find boundaries 📚 What I learned: ✅ Binary search can be customized to find first/last positions ✅ Always recalculate mid after updating low and high ✅ Dry runs are powerful for catching subtle logic bugs Have you tried boundary-based binary search before? Did you use recursion, iteration, or a hybrid approach? Let’s swap strategies 💬 #Day34 #LeetCode #BinarySearch #SearchRange #ProblemSolving #Python #DebuggingJourney #CleanCode #LearnToCode #CodeNewbie #SoftwareEngineering #TechJourney #100DaysOfCode

  • graphical user interface, text, application, chat or text message

To view or add a comment, sign in

Explore content categories