🚀 100 Days DSA Coding Challenge | Day 40 📌 Problem: Implement Queue using Stacks 🧠 What I learned today: • Understanding how to simulate Queue (FIFO) using Stack (LIFO) • Using two stacks to reverse order and maintain queue behavior • Concept of amortized O(1) time complexity • Strengthening fundamentals of data structure design 🛠️ Approach: Use two stacks: in → for pushing elements out → for popping/peeking When out is empty: Transfer all elements from in to out This reverses the order, making it behave like a queue ⏱️ Complexity: Push: O(1) Pop: O(1) (amortized) Peek: O(1) (amortized) Empty: O(1) 💻 Language: Java Today’s takeaway: Understanding how data structures work internally helps us build one structure using another efficiently. 🔄 On to Day 41 🚀 #100DaysOfCode #DSA #Java #Stack #Queue #DataStructures #ProblemSolving #CodingChallenge #LearningInPublic #LeetCode
Implementing Queue with Stacks in Java
More Relevant Posts
-
🚀 100 Days DSA Coding Challenge | Day 39 📌 Problem: Implement Stack using Queues 🧠 What I learned today: • Difference between Stack (LIFO) and Queue (FIFO) • How to simulate a stack using queue operations • Using rotation technique to maintain stack order • Strengthening understanding of data structure design 🛠️ Approach: Use a single queue. When pushing an element: Add it to the queue Rotate the existing elements so the new element comes to the front This ensures: pop() removes the latest element top() returns the most recent element ⏱️ Complexity: Push: O(n) Pop: O(1) Top: O(1) Empty: O(1) 💻 Language: Java Today’s takeaway: Understanding the behavior of data structures allows us to build one structure using another. 🔄 On to Day 40 🚀 #100DaysOfCode #DSA #Java #Stack #Queue #DataStructures #ProblemSolving #CodingChallenge #LearningInPublic #LeetCode
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 22 Today’s focus: Prefix Sum + Fixed Window optimization. Problem solved: • K Radius Subarray Averages (LeetCode 2090) Concepts used: • Prefix Sum • Fixed-size sliding window • Efficient range sum calculation Key takeaway: The goal is to calculate the average of subarrays centered at each index with radius k. A brute-force approach would recompute sums for every index, leading to O(n·k) time. Instead, using prefix sum, we can compute any subarray sum in O(1) time. For each index i, the valid window is: [i - k, i + k] If this window is within bounds, we calculate the sum using prefix sum and divide by (2k + 1). If not enough elements exist on either side, we return -1. This approach reduces the complexity to O(n) and avoids repeated calculations. This problem highlights how prefix sum helps in efficiently handling range-based queries. Continuing to strengthen fundamentals and consistency in DSA problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 72 – DSA Journey | Reversing a Sublist in Linked List Continuing my daily DSA practice, today I worked on a classic linked list problem that tested my understanding of pointer manipulation and in-place reversal. 📌 Problem Practiced: Reverse Linked List II (LeetCode 92) 🔍 Problem Idea: Reverse a portion of a linked list between given positions left and right, without affecting the rest of the list. 💡 Key Insight: Instead of reversing the entire list, the trick is to re-link nodes within the given range by carefully adjusting pointers. 📌 Approach Used: • Use a dummy node to handle edge cases • Traverse to the node just before left • Iteratively move nodes to the front of the sublist • Maintain connections with the remaining list 📌 Concepts Strengthened: • Linked list traversal • Pointer manipulation • In-place reversal • Handling edge cases ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) 🔥 Today’s takeaway: Small pointer changes can drastically transform a data structure — understanding them deeply is key. On to Day 73! 🚀 #Day72 #DSAJourney #LeetCode #LinkedList #Java #ProblemSolving #Coding #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
#CodeEveryday — My DSA Journey | Day 3 🧩 Problem Solved: Combination Sum III (LeetCode #216) 💭 What I Learned: Used backtracking to find all possible combinations of k numbers (1–9) that sum up to a target n. At each step: ✔️ Decided whether to include the current number ✔️ Reduced both the remaining sum (n) and count (k) ✔️ Ensured each number is used only once by moving to the previous index Applied constraints effectively to prune recursion when: Remaining sum becomes negative Required count becomes invalid This improved my understanding of handling multiple constraints (sum + size) simultaneously in recursion. ⏱ Time Complexity: O(C(9,k) 🧠 Space Complexity: O(k) (recursion depth) ⚡ Key Takeaways: ✔️ Backtracking with multiple constraints requires careful pruning ✔️ Fixed input size can significantly reduce complexity ✔️ Choosing + skipping pattern helps explore all valid combinations 💻 Language Used: Java ☕ 📘 Concepts: Backtracking · Recursion · Combinations · Constraint Handling #CodeEveryday #DSA #LeetCode #Java #Backtracking #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
🚀 DSA Every Day – Day 2 📍 Problem: Minimum Distance to Target Element 💡 Platform: LeetCode 🧠 Approach: Brute Force with Bidirectional Traversal 🔍 Problem Summary: Given an array, a target value, and a starting index, find the minimum distance between the start index and any index where the target exists. ⚙️ Approach Explained: Traverse forward (start → end) Traverse backward (start → beginning) For each occurrence of target: Compute distance → |start - i| Maintain the minimum distance 📈 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(1) 🎯 Key Takeaways: Always think in terms of distance minimization problems Bidirectional traversal ensures complete coverage Can be optimized further using early stopping if needed Tell me your approach in comments ? Would love if people would like to join me in this habit for learning every day. 🔥 Progress: Day 2/ 60 #DSA #LeetCode #Java #CodingJourney #ProblemSolving #SoftwareEngineering #60DaysOfCode
To view or add a comment, sign in
-
-
🔥 DSA Challenge – Day 125/360 🚀 📌 Topic: Recursion 🧩 Problem: Combination Sum Problem Statement: Given an array of distinct integers and a target, return all unique combinations where numbers sum up to the target. Same element can be used multiple times. 🔍 Example: Input: candidates = [2,3,6,7], target = 7 Output: [[2,2,3],[7]] 💡 Approach: Backtracking 1️⃣ Step 1 – Start from index 0 and try picking each element 2️⃣ Step 2 – If element ≤ target, include it and reduce target 3️⃣ Step 3 – Backtrack (remove element) and move to next index ✔ Use recursion to explore all possibilities ✔ Reuse same element (stay on same index) ✔ Stop when target becomes 0 (valid answer) ✔ Skip when index reaches end ⏱ Complexity: Time: O(2^n * k) (k = avg length of combination) Space: O(k * x) (x = number of combinations) 📚 Key Learning: Backtracking is all about making choices, exploring, and undoing them efficiently. #DSA #Java #Coding #InterviewPrep #ProblemSolving #TechJourney #360DaysOfCode #LeetCode #Backtracking 🚀
To view or add a comment, sign in
-
-
🚀 Day 99 of DSA Problem Solving Today’s problem: Word Pattern (LeetCode 290) At first glance, this problem looked simple — just matching characters with words. But the real challenge was ensuring a bijective mapping (one-to-one relationship) between pattern characters and words. 🔍 Problem Idea: We need to check if each character in the pattern maps to exactly one word in the string, and each word maps back to only one character. 💡 Key Learning: One-directional mapping is NOT enough We must ensure two-way consistency (character → word AND word → character) HashMaps are powerful for maintaining this mapping efficiently 🧠 Concepts Practiced: HashMap usage String manipulation (split) Bijective mapping logic ⏱ Time Complexity: O(n), where n = number of words 💭 Real Journey Behind the Solution: Initially, I thought using just one HashMap would work, but that led to incorrect mappings. Then I realized the importance of maintaining two HashMaps to ensure no duplication from either side. This problem strengthened my understanding of mapping relationships — a small concept but very powerful in many DSA problems. 🔥 Slowly getting better every day… consistency is the real game. #Day99 #DSA #LeetCode #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
=> Day 20/90 DSA Journey Solved: Count Odd Numbers in an Interval (LeetCode 1523) Instead of using loops, I applied a simple mathematical formula to achieve an O(1) solution. 🔹 Key Idea: Count of odd numbers between low and high can be calculated directly using: -> ((high + 1) / 2) - (low / 2) 🔹 Why this works: -> It efficiently counts odds up to high and subtracts odds before low. 🔹 Example: Input: low = 3, high = 7 Output: 3 (Odd numbers → 3, 5, 7) -> Time Complexity: O(1) -> Space Complexity: O(1) #Java #DSA #LeetCode #ProblemSolving #Coding #Optimization
To view or add a comment, sign in
-
-
Day 92 of #365DaysOfLeetCode Challenge Today’s problem: **Nth Digit (LeetCode 400)** This problem looks simple at first but quickly turns into a pattern-based math challenge. We need to find the **nth digit** in an infinite sequence: `1, 2, 3, ..., 9, 10, 11, 12, ...` 💡 **Key Insight:** Numbers are grouped by digit length: * 1-digit numbers → 9 digits total * 2-digit numbers → 90 × 2 = 180 digits * 3-digit numbers → 900 × 3 = 2700 digits …and so on. Instead of building the sequence, we **skip entire groups** until we land in the correct range. 📌 **Approach:** 1. Determine the digit-length group where `n` lies 2. Identify the exact number containing the nth digit 3. Extract the required digit from that number ⚡ **Time Complexity:** O(log n) ⚡ **Space Complexity:** O(1) **What I learned today:** When dealing with large sequences, **don’t simulate — analyze patterns and jump directly** to the answer. #LeetCode #DSA #CodingChallenge #Java #ProblemSolving #TechJourney #Consistency
To view or add a comment, sign in
-
-
🔥 DSA Challenge – Day 133/360 🚀 📌 Topic: Recursion + Memoization 🧩 Problem: Word Break Problem Statement: Check if a given string can be segmented into space-separated words from a given dictionary. 🔍 Example: Input: s = "leetcode", wordDict = ["leet", "code"] Output: true 💡 Approach: Optimized (Recursion + Memoization) 1️⃣ Step 1 – Convert the word list into a HashSet for fast lookup 2️⃣ Step 2 – Try all substrings starting from current index 3️⃣ Step 3 – Use memoization array to avoid recomputation of same index ⏱ Complexity: Time: O(n³) Space: O(n) 📚 Key Learning: Memoization helps avoid repeated work and turns an exponential problem into a polynomial one. #DSA #Java #Coding #InterviewPrep #ProblemSolving #TechJourney #133DaysOfCode #LeetCode
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