𝗗𝗮𝘆 𝟯𝟴/𝟭𝟬𝟬 | 𝗖𝗼𝗽𝘆 𝗟𝗶𝘀𝘁 𝘄𝗶𝘁𝗵 𝗥𝗮𝗻𝗱𝗼𝗺 𝗣𝗼𝗶𝗻𝘁𝗲𝗿 Day 38 ✅ — When linked lists get complex. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟭𝟯𝟴: Copy List with Random Pointer (Medium) 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: Deep copy a linked list where each node has a next pointer AND a random pointer that can point to any node. The challenge? You can't just copy nodes one-by-one because the random pointers reference nodes you might not have created yet. Solution: 𝗛𝗮𝘀𝗵 𝗠𝗮𝗽. Two passes: Create all nodes and store old → new mapping Connect next and random pointers using the map 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: 👉 First pass: Create clone nodes, map original → clone 👉 Second pass: Set clone.next and clone.random using map 👉 Return cloned head from map Time: O(n), Space: O(n) for the hash map 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: Nine linked list problems. This one showed that hash maps aren't just for arrays—they're powerful for pointer-based structures too. The pattern: when you need to track relationships between objects, think hash map. 𝗖𝗼𝗱𝗲:🔗 https://lnkd.in/gAXPCssx 𝗗𝗮𝘆 𝟯𝟴/𝟭𝟬𝟬 ✅ | 𝟲𝟮 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #LinkedList #HashMap #DeepCopy #DataStructures #CodingInterview #SoftwareEngineer #Java #Algorithms #MediumLevel #Programming
More Relevant Posts
-
While practicing Quick Sort further, I looked into the issue of pivot selection. In the basic implementation, choosing a fixed pivot (like the first element) can sometimes lead to very unbalanced partitions, especially if the array is already sorted or nearly sorted. In such cases, the time complexity can degrade to O(n²). Approach used : Instead of always choosing the same pivot position, the pivot can be selected from a different position (like the middle element or a random index) before performing the partition. The rest of the algorithm remains the same : - choose a pivot - move it to its correct position using partition logic - recursively sort the left and right parts Core Quick Sort logic : static void quickSort(int[] arr, int lo, int hi) { if (lo >= hi) return; int idx = partition(arr, lo, hi); quickSort(arr, lo, idx - 1); quickSort(arr, idx + 1, hi); } Things that became clear : - the pivot choice strongly affects performance - randomized or varied pivot selection helps avoid worst-case patterns - average time complexity remains O(n log n) - recursion stack space is typically O(log n) This made it clear that Quick Sort’s efficiency depends not only on the algorithm itself but also on how intelligently the pivot is chosen. #dsa #algorithms #quicksort #sorting #java #learninginpublic
To view or add a comment, sign in
-
🚀 Day 13/100 — Recursive Deconstruction Today’s Challenge: LeetCode 761 - Special Binary String 🧩 Yesterday was about bits; today was about Structural Grammar. Special Binary Strings aren't just 1s and 0s; they are a language. The Realization: A special string is like a valid set of parentheses. 1 is ( and 0 is ). To find the "lexicographically largest" version, you have to break the string into its most basic components, sort them, and put them back together. My 0ms Optimization Strategy: 1. Mental Mapping: Treated the string as a nested structure. If 1...0 is a special string, I stripped the outer layer, solved the inside, and then re-wrapped it. 2. Greedy Reassembly: Used Collections.sort() with a reverse comparator. In binary, "larger" just means the 1s appear as early as possible. 3. Memory Management: Minimized object creation by identifying "split points" first before committing to substring allocations. Reflection: Sometimes the fastest way to solve a problem isn't to iterate forward, but to look at the problem as a recursive tree. Every "Special" chunk is a node that can be reordered to maximize the total value. 13 days down. The logic is getting deeper. 87 days to go. 🛠️ #Java #DSA #LeetCode #100DaysOfCode #Recursion #StringAlgorithms #SoftwareEngineer #CodingJourney #Optimization
To view or add a comment, sign in
-
-
📌 17. Letter Combinations of a Phone Number (Medium) Today’s problem was all about Backtracking & Recursion. 🧠 Problem Summary Given digits from 2–9, return all possible letter combinations based on phone keypad mapping. Example: Input: "23" Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"] 💡 Key Insight This is a classic Cartesian Product problem. Each digit expands into multiple choices → Perfect use case for Backtracking. 🚀 Approach Create a digit → letter mapping Use recursion to build combinations When current string length == digits length → Add to result Backtrack and explore next possibilities ⏱ Complexity Time: O(4ⁿ) Space: O(n) recursion stack Max combinations = 256 (since n ≤ 4) 🎯 What I Practiced Today ✅ Recursion Tree Thinking ✅ Backtracking Pattern ✅ StringBuilder Optimization ✅ Clean Code Structure Consistency > Motivation 💪 Day 18 completed. #LeetCode #Java #DataStructures #Algorithms #Backtracking #100DaysOfCode
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟰𝟴/𝟭𝟬𝟬 | 𝗖𝗼𝗻𝘃𝗲𝗿𝘁 𝗕𝗶𝗻𝗮𝗿𝘆 𝗡𝘂𝗺𝗯𝗲𝗿 𝗶𝗻 𝗔 𝗟𝗶𝗻𝗸𝗲𝗱 𝗟𝗶𝘀𝘁 𝘁𝗼 𝗜𝗻𝘁𝗲𝗴𝗲𝗿 Day 48 ✅ — Binary logic meets linked list traversal. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟭𝟮𝟵𝟬: Convert Binary Number in a Linked List to Integer (Easy) From LeetCode 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: Each node represents a binary digit (0 or 1). The head is the most significant bit. So this isn’t just a linked list problem — it’s a 𝗯𝗶𝗻𝗮𝗿𝘆 𝘁𝗼 𝗱𝗲𝗰𝗶𝗺𝗮𝗹 𝗰𝗼𝗻𝘃𝗲𝗿𝘀𝗶𝗼𝗻 problem wrapped inside linked list traversal. Two key steps: 1️⃣ Compute the length of the list 2️⃣ Traverse again and apply positional binary weights (2^(length-1)) If a node contains 1, add 2^(position) to the result. Classic bit-weight logic. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: 👉 First pass: calculate linked list length 👉 Second pass: • If node value is 1 → add 2^(length-1) • Decrement length 👉 Handle last node separately Time Complexity: O(n) Space Complexity: O(1) Two traversals, constant space, clean logic. 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: This problem reinforces something important: Linked list questions aren’t always about pointer manipulation. Sometimes they test whether you can layer fundamental concepts (like binary math) on top of traversal mechanics. The more I practice, the clearer patterns become: Traversal is routine. The real thinking is in identifying the hidden concept. 𝗖𝗼𝗱𝗲:🔗 https://lnkd.in/g9TyHZGk 𝗗𝗮𝘆 𝟰𝟴/𝟭𝟬𝟬 ✅ | 𝟱𝟮 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #LinkedList #Binary #DataStructures #CodingInterview #SoftwareEngineer #Java #Algorithms #TimeComplexity #Programming
To view or add a comment, sign in
-
After working through merge-based problems, I spent some time understanding Quick Sort. Unlike Merge Sort, which splits first and merges later, Quick Sort works by placing one element (pivot) in its correct position first, and then recursively sorting the parts around it. The key part of the algorithm is the partition step, where the pivot element is moved to the position where it would appear in the final sorted array. Approach used : - choose a pivot element - count how many elements are smaller than the pivot - place the pivot at its correct index - rearrange elements so that - left side contains values ≤ pivot - right side contains values > pivot - recursively apply the same process to both sides Core quick sort logic : static void quickSort(int[] arr, int lo, int hi) { if (lo >= hi) return; int idx = partition(arr, lo, hi); quickSort(arr, lo, idx - 1); quickSort(arr, idx + 1, hi); } Things that became clear : - the partition step controls the entire algorithm - on average Quick Sort runs in O(n log n) - the worst case can reach O(n²) when the pivot selection is poor - the recursion depth depends on how balanced the partitions are Understanding how the pivot finds its correct position made the whole algorithm much easier to follow. #dsa #algorithms #quicksort #sorting #java #learninginpublic
To view or add a comment, sign in
-
Day 8 – DSA Journey | Arrays 🚀 Today’s problem pushed me to think in terms of recursion 🔁, constraints 🧩, and backtracking ⏪ rather than simple iteration. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • 🧠 Sudoku Solver 🔹 Sudoku Solver 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • 🔁 Used backtracking to try all valid possibilities • 📊 Tracked constraints using boolean matrices for rows, columns, and 3×3 boxes • ✅ Placed a number only if it was valid across all constraints 𝐇𝐨𝐰 𝐢𝐭 𝐖𝐨𝐫𝐤𝐬 • 🔍 Find the first empty cell • 🔢 Try numbers from 1–9 • ▶️ Recurse if placement is valid • ⏪ Backtrack if it leads to an invalid state 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • 💡 Backtracking is about choices, recursion, and undoing • ⚡ Pre-tracking constraints avoid repeated checks • 🧠 Clear constraints make recursion easier to reason about • 🛑 Clean base cases prevent infinite recursion 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • ⏱ Time: Exponential (heavily pruned by constraints) • 📦 Space: O(1) (fixed board + recursion stack) 🧠 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Backtracking isn’t brute force — it’s controlled exploration with rules 🎯 On to Day 9 🔁🚀 #DSA #Arrays #Backtracking #Recursion #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
Day 72/100 – LeetCode Challenge ✅ Problem: #7 Reverse Integer Difficulty: Medium Language: Java Approach: Digit Extraction with Overflow Check Time Complexity: O(log₁₀ x) Space Complexity: O(1) Key Insight: Reverse integer by repeatedly extracting last digit and building result. Critical: Check overflow before final cast using long to detect > Integer.MAX_VALUE or < Integer.MIN_VALUE. Solution Brief: Extracted digits using modulo 10. Built reversed number step by step in long to detect overflow. After loop, divided by 10 to remove extra multiplication. Checked if result fits in 32-bit integer range. Handled sign separately at the end. #LeetCode #Day72 #100DaysOfCode #Math #Java #Algorithm #CodingChallenge #ProblemSolving #ReverseInteger #MediumProblem #Overflow #DigitManipulation #DSA
To view or add a comment, sign in
-
-
Started the Merge Sort section today. Before understanding the full algorithm, the first thing was learning how to merge two already sorted arrays into one sorted array. This small piece of logic is actually the core operation behind Merge Sort. Things that became clear : - using two pointers to traverse both arrays - always placing the smaller element first - continuing until both arrays are fully processed - overall time complexity becomes O(m + n) Main merge logic : static void merge(int[] a, int[] b, int[] c) { int i = 0, j = 0, k = 0; while (i < a.length && j < b.length) { if (a[i] <= b[j]) c[k++] = a[i++]; else c[k++] = b[j++]; } while (i < a.length) c[k++] = a[i++]; while (j < b.length) c[k++] = b[j++]; } What seemed like a small helper function is actually the heart of Merge Sort, because the entire algorithm depends on merging smaller sorted pieces correctly. Understanding this step made the later recursion part much easier to follow. Continuing deeper into Merge Sort and divide-and-conquer next. #dsa #algorithms #mergesort #sorting #java #learninginpublic
To view or add a comment, sign in
-
Day - 88 Maximum Depth of Binary Tree The problem - Given the root of a binary tree, return its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Brute Force - Use level-order traversal (BFS) to visit all nodes level by level, counting the number of levels. This gives O(n) time and O(w) space where w = maximum width, which works but is more complex than needed. Approach Used - •) If root == null, return 0 (empty tree has depth 0). •) Calculate left subtree depth: left = maxDepth(root.left). •) Calculate right subtree depth: right = maxDepth(root.right). •) Return 1 + Math.max(left, right), 1 accounts for current node and Math.max(left, right) selects the deeper subtree. Complexity - Time - O(n), where n = number of nodes. Space - O(h), where h = height of tree. Note - The maximum depth of a binary tree is determined recursively: it's 1 (for the current node) plus the maximum depth of its left and right subtrees. The base case returns 0 for null nodes. This elegant recursive solution naturally follows the tree structure and computes depth in a single pass. #DSA #Java #SoftwareEngineering #InterviewPrep #LearnToCode #CodeDaily #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 6/30 – Applying Binary Search Beyond the Obvious Today’s problem involved finding the **k-th missing positive number** in a sorted array. At first glance, it looks like a counting problem. But instead of iterating through numbers one by one, I approached it using **Binary Search** to optimize performance. ### 💡 Key Insight For any index `i`, the number of missing elements before it can be calculated using: `missing = arr[i] - (i + 1)` This observation transforms the problem into a monotonic condition — which makes it perfect for binary search. By narrowing the search space based on how many numbers are missing at mid, I was able to determine the correct position efficiently. ### 📊 Performance ✅ Accepted (All test cases passed) ⚡ 0 ms runtime (100% performance) 💾 Strong memory efficiency ### 📚 What I Learned Binary Search is not just about searching values — it’s about recognizing patterns where the search space can be reduced logically. The real improvement comes from spotting the mathematical relationship inside the problem. Day 6 complete. Consistency is turning into confidence 💪 #Day6 #30DaysOfCode #LeetCode #Java #BinarySearch #Algorithms #DataStructures #ProblemSolving #CodingJourney #SoftwareEngineering #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