----Continuing my DSA journey---- * Problem: Parse Lisp Expression (736) * Approach: I solved this using recursion + hashmap (scope handling). I parsed the expression and evaluated it based on three cases: • add → evaluate both expressions and sum • mult → evaluate both expressions and multiply • let → assign variables and evaluate in scoped context I used a HashMap to maintain variable values and passed copies to handle nested scopes correctly. * Key Insight: The tricky part was managing variable scope in nested expressions. Each "let" creates a new scope, and variables must be resolved from inner to outer scope. * What I Learned: This problem improved my understanding of parsing complex expressions and scope management. #LeetCode #DSA #Java #Recursion #HashMap #ProblemSolving
Parsing Lisp Expression with Recursion and HashMap
More Relevant Posts
-
DSA Day 11 – Prefix Sum + HashMap Pattern Today I solved the “Subarray Sum Equals K” problem on LeetCode. Problem: Given an array and an integer k, find the total number of subarrays whose sum equals k. Approach Used: -> Used Prefix Sum to keep track of cumulative sum -> Used HashMap to store frequency of prefix sums -> At each step, checked if (currentSum - k) exists in map -> If yes → added its frequency to count Time Complexity: -> O(n) Space Complexity: -> O(n) Key Learning: -> Prefix Sum helps convert subarray problems into simple calculations -> HashMap allows constant time lookup for previous sums -> Instead of checking all subarrays (O(n²)), we optimize to O(n) What I Realized: Earlier I used brute force for subarrays, but today I understood how prefix sum + hashmap eliminates nested loops completely. This pattern is powerful for many subarray problems. Thanks to Pulkit Aggarwal sir for guiding me in DSA and helping me understand problem-solving patterns. Consistency is turning into real problem-solving ability. #DSA #LeetCode #Java #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Day 39 of 100: #100DaysDSAChallenge ✅ 💡 Topic: Array Problems (Medium) - Continued 📚 Resource: Striver A2Z DSA Sheet + Playlist 🖥️ Language: Java 📌 What I did today: Solved Count Inversions in an Array — two approaches from brute force to the elegant merge sort trick. ✅ Problem Solved: • Count Inversions in an Array 🔑 Key Learnings: → Approach 1 — Brute Force (Nested Loop): • Check every pair (i, j) where i < j and arr[i] > arr[j] • Count all such pairs directly • Time: O(n²) | Space: O(1) → Approach 2 — Modified Merge Sort: • During the merge step, when a left element is greater than a right element, all remaining left elements also form inversions with it • Count inversions while sorting — no extra traversal needed • Time: O(n log n) | Space: O(n) 💡 The merge sort approach is the real insight — it counts inversions naturally during the merge step. What looks like a comparison problem is actually hiding inside a sorting algorithm. 🔥 61 more days to go! #Day39 #DSA #100DaysOfDSA #Java #StriverA2Z #Arrays #MergeSort #Inversions #CodingChallenge #DSAJourney
To view or add a comment, sign in
-
-
**Day 107 of #365DaysOfLeetCode Challenge** Today’s problem: **4Sum II (LeetCode 454)** This problem looks like brute force at first glance… but a direct 4-loop solution would be **O(n⁴)** So the real challenge is: 👉 How do we optimize using smart preprocessing? 💡 **Core Idea: Meet in the Middle** Instead of checking all 4 arrays together: 1️⃣ Compute all possible sums of `nums1 + nums2` 2️⃣ Store frequencies in a HashMap 3️⃣ For every pair in `nums3 + nums4`, look for its negative value If: `a + b + c + d = 0` Then: `a + b = -(c + d)` 📌 **Approach:** * Build map of pair sums from first two arrays * Traverse pair sums from last two arrays * Add matching frequency from map ⚡ **Time Complexity:** O(n²) ⚡ **Space Complexity:** O(n²) **What I learned today:** When brute force is too expensive, split the problem into halves. 💭 **Key Takeaway:** This pattern appears often in interviews: 👉 4Sum / KSum 👉 Pair matching 👉 Meet in the Middle 👉 HashMap frequency counting Sometimes optimization is not about faster loops… It’s about changing the way you think #LeetCode #DSA #HashMap #MeetInTheMiddle #CodingChallenge #ProblemSolving #Java #TechJourney #Consistency
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 35 Today’s focus: Binary Search with index patterns. Problem solved: • Single Element in a Sorted Array (LeetCode 540) Concepts used: • Binary Search • Index parity (even/odd pattern) • Search space reduction Key takeaway: The array is sorted and every element appears twice except one. A key observation: Before the single element, pairs start at even indices After the single element, this pattern breaks. Using binary search: • If mid is even and nums[mid] == nums[mid + 1], the single element lies on the right side • Else, it lies on the left side (including mid) By leveraging this pattern, we can find the answer in O(log n) time and O(1) space. Continuing to strengthen binary search intuition and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Day 60 – Search a 2D Matrix Solved a problem to efficiently search for a target in a sorted matrix using binary search. Key Learnings: Learned to treat a 2D matrix as a flattened sorted array Understood index mapping from 1D to 2D (row = mid / cols, col = mid % cols) Strengthened problem-solving skills in optimized searching techniques #DSA #Java #BinarySearch #Matrix #ProblemSolving #CodingPractice
To view or add a comment, sign in
-
-
🚀 Day 32 of #128DaysOfCode Today I explored an efficient way to find the square root of a number without using built-in functions. Instead of the usual brute force or binary search, I implemented Newton’s Method (Babylonian Method) — a powerful mathematical approach that converges very fast. 🔹 The idea is to continuously improve our guess using: r = (r + x / r) / 2 This method quickly narrows down to the correct integer square root, making it both optimized and elegant. 💡 Key Takeaways: - Learned how mathematical concepts can optimize coding problems - Understood the importance of avoiding overflow using "long" - Practiced writing clean and efficient Java code 🔥 Consistency is the real game changer. Step by step, improving logic and problem-solving skills! #Java #DSA #CodingJourney #ProblemSolving #Consistency #LearnInPublic
To view or add a comment, sign in
-
-
#CodeEveryday — My DSA Journey | Day 14 🧩 Problem Solved: Search a 2D Matrix (LeetCode #74) 💭 What I Learned: Used binary search by treating the 2D matrix as a flattened sorted array. Key idea: 👉 Convert 1D index → 2D coordinates using: row = mid / number_of_columns col = mid % number_of_columns At each step: ✔️ Calculated row and column from mid ✔️ Compared matrix value with target ✔️ Adjusted search space accordingly This approach avoids nested loops and gives optimal performance. ⏱ Time Complexity: O(log (m × n)) 🧠 Space Complexity: O(1) ⚡ Key Takeaways: ✔️ 2D problems can often be reduced to 1D using indexing tricks ✔️ Binary search works whenever the data is globally sorted ✔️ Index mapping is a powerful technique in matrix problems 💻 Language Used: Java ☕ 📘 Concepts: Binary Search · Matrix · Index Mapping · Optimization #CodeEveryday #DSA #LeetCode #Java #BinarySearch #Matrix #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
#CodeEveryday — My DSA Journey | Day 2 🧩 Problem Solved: Combination Sum II (LeetCode) 💭 What I Learned: Used backtracking to generate all unique combinations whose sum equals the target. Sorted the array initially to efficiently handle duplicates and enable pruning. At each step: ✔️ Skipped duplicate elements using i > n && nums[i] == nums[i-1] ✔️ Stopped recursion early when the current element exceeded the target ✔️ Moved to the next index (i+1) to avoid reusing elements Strengthened my understanding of handling duplicates in recursion and optimizing search space. ⏱ Time Complexity: O(2ⁿ)*k (k is avg length of each combination) 🧠 Space Complexity: O(n) ⚡ Key Takeaways: ✔️ Sorting helps in both pruning and duplicate handling ✔️ Skipping duplicates is crucial for unique combinations ✔️ Backtracking becomes efficient with early stopping conditions 💻 Language Used: Java ☕ 📘 Concepts: Backtracking · Recursion · Arrays · Duplicate Handling · Pruning #CodeEveryday #DSA #LeetCode #Java #Backtracking #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
🚀 Day 24/100 — #100DaysOfLeetCode Another step forward in strengthening string problem-solving skills 💻🔥 ✅ Problem Solved: 🔹 LeetCode 28 — Find the Index of the First Occurrence in a String 💡 Concepts Used: String Matching Two Pointer / Brute Force Comparison 🧠 Key Learning: The task was to locate the first occurrence of a pattern (needle) inside another string (haystack). I practiced comparing characters sequentially and handling mismatches carefully while traversing the string. ⚡ Insight Gained: Even simple-looking problems help build a strong foundation for advanced string searching algorithms like KMP and Rabin–Karp. Understanding basic string matching logic is essential before moving to optimized pattern-search techniques. Consistency continues 🚀 #100DaysOfLeetCode #LeetCode #DSA #Strings #Algorithms #Java #ProblemSolving #CodingJourney #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
Day 58/100 | #100DaysOfDSA 🔄🔍 Today’s problem: Search in Rotated Sorted Array A twist on Binary Search with a rotated array. Key idea: Even though the array is rotated, one half is always sorted. Approach: • Use binary search • Find mid element • Check which half is sorted (left or right) • Decide if target lies in the sorted half • Narrow the search accordingly Why it works: At every step, we eliminate half of the search space just like standard binary search. Time Complexity: O(log n) Space Complexity: O(1) Big takeaway: Understanding the structure of the problem helps adapt classic algorithms like binary search. Rotation doesn’t break order — it just shifts it. 🔥 Day 58 done. #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearch #Arrays #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
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