LeetCode — But Did You Think About the How? #ContainsDuplicate | NeetCode 150 Most devs solve "Contains Duplicate" - https://lnkd.in/gq84sj37 and move on. But this problem is a great lens for thinking about trade-offs — and that's exactly what interviewers are watching for. Here are my two solutions: Solution 1 — Sort + Linear Scan class Solution { public boolean hasDuplicate(int[] nums) { Arrays.sort(nums); for (int i = 1; i < nums.length; i++) { if (nums[i] == nums[i - 1]) return true; } return false; } } Time: O(n log n) | Space: O(1) (in-place sort) Solution 2 — Stream + Distinct class Solution { public boolean hasDuplicate(int[] nums) { return Arrays.stream(nums).distinct().count() < nums.length; } } Time: O(n) | Space: O(n) My repository - https://lnkd.in/g_Ke_eqR Key Insight: One liner looks cleaner. But it costs you extra memory. The sort-based approach trades time for space — and modifies the original array. Neither is universally "better." The right answer depends on your constraints: → Memory-constrained system? Go with Sort. → Read-only array or immutable input? Stream wins. → Need early exit on first duplicate? Both support it — but stream is lazy, so it does too This is the kind of thinking that separates good developers from great ones in interviews. What's your go-to approach? Drop it below #Java #LeetCode #NeetCode #DSA #CodingInterview #SpringBoot #SoftwareEngineering #ProblemSolving #100DaysOfCode
Contains Duplicate Problem Solution Trade-Offs Java LeetCode
More Relevant Posts
-
Most software projects don’t fail because of bad code. They fail because there’s no real ownership. That’s why companies work with software development partners, not task-based vendors. At D2 WebTech, we own the product end-to-end. Let’s build it right. #SoftwareDevelopment #TechPartner #Python #ReactJS
To view or add a comment, sign in
-
🧠 LeetCode POTD — Simple Problem, One Small Trick 2515. Shortest Distance to Target String in a Circular Array At first glance, this feels very straightforward. 👉 Find all positions of target 👉 Compute distance from startIndex (from both directions) 👉 Take the minimum Done. But then I thought… Why am I finding all positions first? We're allowed to move left or right, one step at a time. ━━━━━━━━━━━━━━━━━━━ 💡 So instead, I started from startIndex and expanded in both directions simultaneously. 📌 The moment we hit the target → that distance is already the minimum. No need to check anything else. ━━━━━━━━━━━━━━━━━━━ The only tricky part? Handling the circular nature of the array. 💡 Clean trick — both directions in the same loop: → Right: (start + i) % n → Left: (start - i + n) % n That's it. ━━━━━━━━━━━━━━━━━━━ 📌 What I liked about this problem: Not about complexity. Not about fancy algorithms. Just about finding a clean way to move in both directions. ✅ Sometimes the hardest part isn't solving… ✅ It's writing a simple idea cleanly. Curious if someone approached this differently 👀 #LeetCode #ProblemSolving #SoftwareEngineering #DSA #C++ #Java #SDE
To view or add a comment, sign in
-
-
100 Leetcode Problems solved. When I started, even “easy” problems felt confusing. I used to jump straight into coding and get stuck more often than not. Over time, I changed my approach. Instead of rushing to solutions, I started focusing on: 🔹breaking problems down before coding 🔹identifying patterns (especially in stacks, arrays, and linked lists) 🔹understanding time & space complexity 🔹learning how to optimize brute force approaches That shift made a big difference. Now, I’m not just solving problems — I’m thinking more like how I would in an interview setting: “How can I explain this clearly?” “Is this the most optimal approach?” “What edge cases am I missing?” 100 is still just the beginning, but it feels good to see progress from where I started. Next goal: improve problem-solving speed + consistency, and keep strengthening core DSA concepts. If you're on the same path — keep going. It’s slow, but it adds up. #LeetCode #DSA #InterviewPrep #ProblemSolving #Java #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 8 of Java with DSA Journey 🚀 📌 Topic: FizzBuzz (LeetCode 412) 💬 "Clean code is not written by following rules, but by following logic." ✨ What I learned today: 🔹 Condition Priority Matters Checking FizzBuzz (divisible by both 3 & 5) first is crucial. Order defines correctness. 🔹 String Handling Used Integer.toString(i) for clean output formatting. 🔹 Code Readability Even simple problems test how clearly you structure your logic. 🔹 Complexity Awareness ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) (excluding output list) 🧠 Problem Solved: ✔️ FizzBuzz 🎨 Visualizing the Logic (Decision Tree) Each number flows through conditions like a filter. It stops at the first true condition, which makes ordering critical. 💡 Key Insight: FizzBuzz may look simple, but it teaches precise logical structuring and modular arithmetic (%). 👉 The “15 Test”: If we check i % 3 == 0 first, 15 becomes "Fizz" and skips "Buzz". By checking (i % 3 == 0 && i % 5 == 0) first, we correctly get "FizzBuzz". ⚡ Interview Insight (Scalability Twist): What if we add a new condition like 7 → "Bazz"? Instead of messy if-else, use string concatenation: String currentStr = ""; if (i % 3 == 0) currentStr += "Fizz"; if (i % 5 == 0) currentStr += "Buzz"; if (currentStr.isEmpty()) currentStr += i; answer.add(currentStr); ✅ Easier to extend ✅ Cleaner logic ✅ More maintainable 🔑 Takeaway: Consistency beats complexity. Showing up daily builds real problem-solving skills. #DSA #LeetCode #Java #CodingJourney #ProblemSolving #Day8 #CleanCode #SoftwareEngineering #FizzBuzz #Array #InterviewPrep #Optimization #MCA #lnct #100DaysOfCode #SoftwareEngineering #InPlaceAlgorithms #TechLearning #JavaDeveloper
To view or add a comment, sign in
-
-
Day12 of leetcode 100days 💡 Problem Solved: Binary Search (LeetCode 704) 👉 Key Idea: Work on a sorted array, compare the middle element, and eliminate half of the search space each time. Takeaways: ✔️ Always use low + (high - low)/2 to avoid overflow ✔️ Loop condition should be low <= high ✔️ Works only on sorted arrays ✔️ Foundation for advanced problems like rotated arrays & bounds Binary Search is not just a problem — it’s a pattern that unlocks multiple interview questions. #DataStructures #Algorithms #Java #CodingInterview #LeetCode #BinarySearch #DSA #SoftwareEngineering #toronto #tech #2026 #sde #canada #tech #100daysleetcode #coders
To view or add a comment, sign in
-
-
🚀 200 problems on LeetCode — but the real growth isn’t just a number. Somewhere along the journey, my approach changed. I stopped asking: ❌ “How do I solve this problem?” And started asking: ✅ “Why does this approach work?” ✅ “What pattern is behind this?” ✅ “Where else can I apply this?” That shift made all the difference. 📈 What I’ve built so far: ✔ Stronger problem-solving mindset ✔ Better understanding of data structures ✔ Pattern recognition across problems ✔ More structured debugging approach Now, I don’t just solve problems — I understand them. Still learning. Still improving. But now with clarity and direction. #LeetCode #DSA #ProblemSolving #CodingJourney #Java
To view or add a comment, sign in
-
-
⚡ Code is Easy. Thinking is Hard. Over time, I’ve realized that writing code is just one part of being a developer. The real challenge is how you think before you write it. Understanding the problem, designing the right approach, and considering scalability, performance, and edge cases—that’s where the real engineering happens. Lately, I’ve been focusing more on: 🔹 Breaking down complex problems 🔹 Writing code that’s easy to maintain 🔹 Thinking about long-term impact, not just quick fixes Because good code works. But great code lasts. Always learning, always improving 🚀 #SoftwareEngineering #FullStackDeveloper #Java #SystemDesign #CleanCode #GrowthMindset
To view or add a comment, sign in
-
Topic: Asking Better Questions Good engineers don’t just give answers. They ask better questions. Before solving a problem, asking the right questions matters: • What is the actual problem? • What are the constraints? • What is the expected outcome? • Are there edge cases? Better questions lead to: • Clearer understanding • Better solutions • Fewer reworks Sometimes the biggest mistake is solving the wrong problem. Clarity starts with curiosity. What’s one question that helped you solve a tough problem? #SoftwareEngineering #ProblemSolving #EngineeringMindset #Java #BackendDevelopment
To view or add a comment, sign in
-
Tried solving LeetCode #27 — Remove Element today, and it reminded me how important problem constraints are in coding interviews. 👀 At first, I focused only on removing the target values correctly. Later, I understood that the real challenge was doing it efficiently without using extra memory. That shift in thinking introduced me to a useful concept: ✨ writing solutions that are both correct and optimized. A small problem, but a good reminder that: clean logic matters optimization matters understanding patterns matters even more Slowly getting better at recognizing efficient approaches for array problems. 🚀 #LeetCode #Java #DSA #CodingPractice #ProgrammerLife #SoftwareDeveloper #TechJourney
To view or add a comment, sign in
-
-
Day 14 of my Java + DSA Journey 🚀 This “Easy” Problem Taught Me More Than Expected Most people solve problems… Few understand what actually happens behind the scenes. 📌 Problem Solved LeetCode 1389 Today, I worked on Create Target Array in the Given Order—and it turned out to be a lesson in real-world data handling. 💡 What looks simple isn’t always simple At first glance, it’s just inserting numbers at given indices. But internally? →Every insertion triggers element shifting in memory → Which directly impacts performance and scalability 🧠 Key Learnings 🔹 Insertion ≠ Replacement Setting a value is cheap. Maintaining order during insertion is where complexity kicks in. 🔹 Why ArrayList matters Using: list.add(index, element); helped me focus on logic while Java handled internal shifting. 🔹 Hidden Time Complexity Loop looks like O(n) But insertion is O(n) → Overall O(n²) This is the kind of detail interviewers actually look for. 🎯 Real Engineering Insight This problem connects directly to real systems: ✔️ Inserting logs in order ✔️ Managing dynamic datasets ✔️ Handling ordered user inputs These are not just “DSA problems”—they’re backend realities. 📈 Build in Public Right now, I’m focused on: ✔️ Strengthening fundamentals ✔️ Understanding internal working ✔️ Writing clean, efficient Java code Transitioning from: ❌ Just solving problems ✅ Thinking like a backend engineer 🔥 Hard Truth If you only solve LeetCode without understanding how things work internally… → You’re preparing for syntax, not engineering. #BuildInPublic #JavaDeveloper #LeetCode #DSA #BackendDevelopment #ProblemSolving #CodingJourney #ArrayList #InterviewPrep #100DaysOfCode #MCA #LNCT #Java #Day9 #CleanCode #Array #Optimization #lnct #100DaysOfCode #SoftwareEngineering #Algorithms #InPlaceAlgorithms #TechLearning
To view or add a comment, sign in
-
More from this author
Explore related topics
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