Day 62 of my 365-Day DSA Challenge 🔥 Solved LC 7 — Reverse Integer (Medium) | Runtime: 1ms ⚡ But here's the real story --> I got accepted on my first attempt, and then I spotted my own bug. My original solution had no overflow guard. The line: rev = rev * 10 + original % 10 ...could silently overflow an int, and I wouldn't even know it. LeetCode just happened not to hit that edge case hard enough. The fix? Check BEFORE multiplying: → If rev > MAX_VALUE / 10, it will overflow → return 0 → If rev == MAX_VALUE / 10 and next digit > 7 → return 0 (Same logic for the negative side with MIN_VALUE and -8) This is why understanding WHY your code works matters more than just seeing "Accepted." A passing test ≠ correct code. 365 days. One problem at a time. 💪 #DSA #LeetCode #Java #365DayChallenge #SoftwareEngineering #CodingJourney
Reversing Integers with Overflow Guard in Java
More Relevant Posts
-
Day 42 of Daily DSA 🚀 Solved LeetCode 921: Minimum Add to Make Parentheses Valid ✅ Problem: Given a parentheses string s, return the minimum number of moves required to make it valid. Rules: A valid string has balanced opening and closing brackets You can insert a parenthesis at any position Return the minimum additions needed Approach: Used a Stack + Counter approach to track unmatched parentheses. Steps: Traverse the string Push '(' onto stack If ')' and stack has '(' → pop Else → increment counter (unmatched closing) At the end → remaining stack size = unmatched openings Result = unmatched openings + unmatched closings ⏱ Complexity: • Time: O(n) • Space: O(n) 📊 LeetCode Stats: • Runtime: 1 ms (Beats 67.19%) ⚡ • Memory: 43.08 MB Nice extension of the Valid Parentheses problem, focusing on fixing invalid cases instead of just checking them. #DSA #LeetCode #Java #Stack #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🔥 Day 58 of DSA Journey Solved the classic 3Sum (LeetCode 15) problem today. 💡 Key Learnings: Sorting simplifies the problem structure Fix one element and apply the two-pointer approach Handling duplicates correctly is crucial to avoid redundant results ⏱️ Complexity: Time: O(n²) Space: O(1) (excluding output) 📊 Result: Runtime: 33 ms (Beats 73.75%) Strengthened understanding of two-pointer pattern This problem reinforced an important pattern: 👉 Reduce 3Sum → 2Sum using two pointers On to the next challenge 🚀 #DSA #LeetCode #CodingJourney #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
“Most people try to overcomplicate this one… but the simplest approach wins.” Day 69 — LeetCode Progress Problem: Height Checker Required: Given an array of student heights, return the number of indices where the heights are not in the expected non-decreasing order. Idea: If we sort the array, we get the expected order. Now just compare the original array with the sorted version — mismatches are the answer. Approach: Create a copy of the original array Sort the copied array Traverse both arrays: Compare elements at each index If they differ → increment count Return the count Time Complexity: O(n log n) Space Complexity: O(n) #LeetCode #DSA #Java #Arrays #Sorting #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀100 Days of Code Day - 17 Problem Solved: Letter Combinations of a Phone Number Today I worked on an interesting backtracking problem where I generated all possible letter combinations from a given digit string (2–9), based on the classic phone keypad mapping. 🔍 Key Learnings: • Understood how recursion helps explore all possible combinations • Strengthened my knowledge of backtracking techniques • Improved problem-solving and logical thinking 💡Approach: Used a recursive backtracking method to build combinations step by step, exploring all possible letter mappings for each digit. 📌 Example: Input: "23" Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"] This problem is a great example of how powerful recursion can be when dealing with combinations. #Java #DSA #Backtracking #ProblemSolving #CodingJourney #LeetCode
To view or add a comment, sign in
-
-
Day 84 of My DSA Journey Today’s problem: Counting Bits Given a number n, the task is to return an array where each index i contains the number of 1’s in the binary representation of i. 🔍 Example: Input: n = 5 Output: [0, 1, 1, 2, 1, 2] 💡 Key Insight: Instead of counting bits every time, we reuse previous results: i >> 1 → removes last bit i & 1 → checks if last bit is 1 So, 👉 ans[i] = ans[i >> 1] + (i & 1) ⚡ This reduces time complexity to O(n) (single pass!) 📈 What I learned today: Dynamic Programming can simplify repeated computations Bit manipulation makes problems faster and cleaner Small patterns can lead to big optimizations #Day84 #DSA #Java #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 36 of my DSA Journey 📌 Problem: 560. Subarray Sum Equals K Given an array and a value k, we need to find how many subarrays (continuous parts of the array) add up to k. 💡 My Approach (Simple Explanation): Instead of checking all subarrays (which is slow), I used the prefix sum + HashMap trick. Keep adding elements to get a running sum Check if (current sum - k) was seen before If yes, it means a valid subarray exists Store prefix sums in a map to reuse quickly This makes the solution efficient ⚡ ✅ Result: Accepted ⏱ Runtime: 0 ms ✨ Small optimizations like this really show how powerful concepts can simplify problems! #Day36 #DSA #CodingJourney #Java #LeetCode #ProblemSolving
To view or add a comment, sign in
-
-
Another step forward in my DSA journey 🚀 After going through the basics, I’ve now started Arrays — and today was all about understanding how they actually work under the hood. 💡 Key takeaway: Arrays are stored in the heap, but each array itself is allocated as a continuous block of memory — making indexing fast and efficient. Also explored: • Why arrays are needed • Internal working & memory representation • Dynamic memory allocation • null & default values in Java • for-each loop & toString() • Arrays of objects • Passing arrays in functions Small concept, big clarity. This is where real problem-solving starts. #DSA #Java #LearningInPublic #Consistency #CodingJourney
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 28 Today’s focus: Binary Search for minimum in rotated arrays. Problem solved: • Find Minimum in Rotated Sorted Array (LeetCode 153) Concepts used: • Binary Search • Identifying unsorted half • Search space reduction Key takeaway: The goal is to find the minimum element in a rotated sorted array. Using binary search, we compare the mid element with the rightmost element: • If nums[mid] > nums[right] → minimum lies in the right half • Else → minimum lies in the left half (including mid) This works because the rotation creates one unsorted region, and the minimum always lies in that region. By narrowing the search space each time, we achieve O(log n) time complexity. This problem highlights how slight modifications in array structure still allow binary search to work efficiently with the right observations. Continuing to strengthen binary search patterns and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 DSA Journey Update: 7/75 Solved another problem today: Product of Array Except Self 💡 Key Idea: Used prefix (left) and suffix (right) products to build the answer without using division and in O(n) time. 🔍 Example: Input: [1,2,3,4] Output: [24,12,8,6] ⚡ Learning: Optimized approach using two passes Space optimization by reusing the same array Stronger understanding of prefix/suffix patterns Step by step progress… consistency matters more than speed 🔥 #DSA #Java #LeetCode #CodingJourney #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
🚀 DSA Preparation 💪 Solved an interesting Array + Sliding Window problem on maximizing score. Instead of picking cards directly, learned to think in reverse by finding the minimum subarray to exclude 🔥 This approach helps in optimizing the solution efficiently 🚀 🧠 Problem 🔎 Maximum Points You Can Obtain from Cards Given an array cardPoints and an integer k, you can take cards from the beginning or end. 👉 You must take exactly k cards. 👉 Return the maximum score possible. Example Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Input: cardPoints = [2,2,2], k = 2 Output: 4 Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Improving DSA with better thinking and optimization 🚀 #DSA #LeetCode #SlidingWindow #Arrays #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development