🚀 Day 10 of DSA Practice: Merge Two Sorted Arrays Today’s problem is a classic and super important for interviews 👇 🧩 Problem Given two sorted arrays, merge them into a single sorted array. 🔍 Example Input: Array 1: [1, 3, 5] Array 2: [2, 4, 6] Output: [1, 2, 3, 4, 5, 6] 💡 Approach 1: Two Pointers (Optimal) Since both arrays are already sorted, we can use a two-pointer technique. 👉 Compare elements from both arrays and pick the smaller one each time. ✅ Time Complexity: O(n + m) ✅ Space Complexity: O(n + m) def merge_sorted_arrays(arr1, arr2): i, j = 0, 0 merged = [] while i < len(arr1) and j < len(arr2): if arr1[i] < arr2[j]: merged.append(arr1[i]) i += 1 else: merged.append(arr2[j]) j += 1 # Add remaining elements merged.extend(arr1[i:]) merged.extend(arr2[j:]) return merged 💡 Approach 2: Using Built-in Sort (Not Optimal) Merge both arrays and then sort. ❌ Time Complexity: O((n+m) log(n+m)) def merge_sorted_arrays(arr1, arr2): return sorted(arr1 + arr2) ⚡ Key Takeaways ✔️ Two-pointer approach is efficient and interview-friendly ✔️ Works because arrays are already sorted ✔️ Foundation for advanced topics like merge sort #Python #CodingJourney #30DaysOfCode #LearnToCode #Programming #Developers #ProblemSolving #PythonBasics
Merge Two Sorted Arrays in Python
More Relevant Posts
-
🔥 DSA Series | Day 11 We all have seen the classic Two Sum / Pair Sum problem everywhere — even trending on LinkedIn. But today, a small twist struck my mind… 🤔 👉 What if we need to find ALL UNIQUE pairs that sum to a target? 💡 Problem Input: arr = [1,1,2,2,3,3] target = 4 👉 Expected Output: [[1,3], [2,2]] 🚀 Approach (Two Pointers + Sorting) Instead of brute force ❌ (O(n²)), we: Sort the array Use two pointers (i, j) Move intelligently based on sum Skip duplicates to ensure uniqueness 🧠 Code class Solution: def uniquePairs(self, arr: list[int], target: int) -> list[list[int]]: arr.sort() n = len(arr) i = 0 j = n - 1 res = [] while i < j: total = arr[i] + arr[j] if total == target: res.append([arr[i], arr[j]]) i += 1 j -= 1 # 🔁 Skip duplicates while i < j and arr[i] == arr[i - 1]: i += 1 while i < j and arr[j] == arr[j + 1]: j -= 1 elif total < target: i += 1 else: j -= 1 return res if res else [[-1, -1]] ⚡ Complexity ⏱️ Time → O(n log n) (sorting dominates) 📦 Space → O(1) (excluding output) 🔥 Key Insight 💡 Removing duplicates smartly during traversal is more efficient than removing them beforehand. 🧠 Takeaway Sometimes a small twist in a common problem → turns it into a great interview question What would you choose? 🤔 🔹 Hashing (O(n)) 🔹 Two Pointers (O(n log n), cleaner) Let’s discuss 👇 #DSA #Coding #Python #InterviewPrep #ProblemSolving #100DaysOfCode #Tech #Learning
To view or add a comment, sign in
-
-
94 pages. Every URL, title & description. Mapped in 3.6 seconds flat. Here's how I used Firecrawl to build a course search tool for students , without touching a single line of HTML. What I got back instantly: → Every course page → Faculty-wise pages with batch details → Descriptions ready for semantic search or RAG I fed this directly into an LLM-powered search index. Now students can ask: "Show me all batches subject-wise for May 2026" ...and get accurate results, without touching a single HTML tag. This is what I mean when I say Firecrawl is infrastructure for AI, not just a scraper. The [/map](https://lnkd.in/dsJ7c4nH) endpoint is criminally underrated. One call. Structured output. Production-ready. What website would you map first? Drop it below — I'll show you what the output looks like. 👇 #Firecrawl #WebScraping #RAG #AItools #Python #BuildInPublic #EdTech
To view or add a comment, sign in
-
-
🚀 Day 15 of DSA Grind — Sliding Window Maximum (Hard) 🔥 Today’s problem pushed my thinking to the next level — not just solving, but optimizing. 💡 Problem: Given an array, find the maximum in every sliding window of size k. ⚡ Naive Approach: Check every window → O(n * k) ❌ (Too slow) 🔥 Optimized Approach (Deque / Monotonic Queue): Maintain elements in decreasing order Remove useless elements Front always gives maximum ✅ Time Complexity: O(n) ✅ Space Complexity: O(k) 🧠 Key Learning: This problem teaches how to avoid recomputation and use data structures smartly — a must-know pattern for interviews. 📌 Example: Input: [1,2,1,0,4,2,6], k = 3 Output: [2,2,4,4,6] 💬 Big Takeaway: Don’t just solve — optimize like an engineer. Consistency > Motivation 💯 #DSA #LeetCode #SlidingWindow #Python #CodingJourney #TechInterview #100DaysOfCode #SoftwareEngineering #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Cracked “Top K Frequent Elements” with an Optimal Approach! Today I solved one of the most important interview problems on LeetCode using an efficient Bucket Sort approach (O(n)) — and got it accepted ✅ 🔍 Problem Insight: Instead of sorting (which takes O(n log n)), I used frequency as an index to directly access elements with higher occurrence. 💡 Key Learnings: How to reduce time complexity from O(n log n) → O(n) Using hashmaps (Counter) for frequency counting Applying bucket sort for optimization Writing clean and interview-ready code ⚡ Complexity Analysis: Time Complexity: O(n) (Frequency count + bucket fill + traversal) Space Complexity: O(n) (Hashmap + bucket storage) ⚡ Performance: Runtime: 10 ms 🧠 Approach Summary: Count frequency of elements Store elements in buckets based on frequency Traverse from highest frequency to get top K elements 📌 Consistency > Perfection Every problem solved is one step closer to mastering DSA. #DataStructures #Algorithms #Python #LeetCode #CodingJourney #ProblemSolving #TechGrowth #Consistency #Learning #DSA
To view or add a comment, sign in
-
-
"SHINE" In software, complexity is the friction that creates brilliance. Every bug is a facet being cut; every optimized query is a layer of dust removed. It's aren't just writing a script—it is a beacon in the dark. Minimize the opacity. Maximize the output. Let the code be the light Don't just execute code. Ignite it. Don't just build a tool. Build a beacon. Code with logic, but design with soul. #code #Project #ML #AI #MultimodalAI #NeonAesthetic #Glassmorphism #Design #WebDesign #TechInnovation #Python
To view or add a comment, sign in
-
🚀 𝗦𝘁𝗿𝗶𝗻𝗴 𝗖𝗼𝗺𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝗦𝗼𝗹𝘃𝗲𝗱 𝗶𝗻 𝟯 𝗗𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝘁 𝗪𝗮𝘆𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝗦𝘁𝗮𝘁𝗲𝗺𝗲𝗻𝘁: Given an array of characters, compress the string by replacing repeated characters with the character followed by its count. 🔗 𝗚𝗶𝘁𝗛𝘂𝗯 𝗖𝗼𝗱𝗲 👉https://lnkd.in/gap5HkW2 Example: Input: ["a","a","b","b","c","c","c"] Output: ["a","2","b","2","c","3"] 𝗜 𝗶𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁𝗲𝗱 𝟯 𝗮𝗽𝗽𝗿𝗼𝗮𝗰𝗵𝗲𝘀: 1️⃣ 𝗕𝗿𝘂𝘁𝗲 𝗙𝗼𝗿𝗰𝗲 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 Create a separate result array Count repeated characters Append character and count Time Complexity: O(n) Space Complexity: O(n) 2️⃣ 𝗕𝗲𝘁𝘁𝗲𝗿 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 Use two pointers Cleaner logic with left and right pointers Still uses extra space Time Complexity: O(n) Space Complexity: O(n) 3️⃣ 𝗢𝗽𝘁𝗶𝗺𝗶𝘇𝗲𝗱 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 Use in-place modification Maintain a write pointer Most memory efficient solution Time Complexity: O(n) Space Complexity: O(1) 𝗧𝗵𝗲 𝗼𝗽𝘁𝗶𝗺𝗶𝘇𝗲𝗱 𝗮𝗽𝗽𝗿𝗼𝗮𝗰𝗵 𝗶𝘀 𝗯𝗲𝘀𝘁 because it avoids extra memory usage and works directly on the input array. 𝗞𝗲𝘆 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴: Always try to solve a problem in multiple ways: First for correctness Then for readability Finally for optimization 𝗧𝗵𝗶𝘀 𝗶𝘀 𝗮 𝗴𝗿𝗲𝗮𝘁 𝗶𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗽𝗿𝗼𝗯𝗹𝗲𝗺 because it tests: ✔️ Arrays ✔️ Two Pointers ✔️ String Manipulation ✔️ Time and Space Complexity Data Structures and Algorithms and Python are all about improving problem-solving skills step by step. #Python #DSA #CodingInterview #ProblemSolving #Algorithms #100DaysOfCode #SoftwareEngineering #Programming #LeetCode #Coding
To view or add a comment, sign in
-
-
Master the Pythonic Way: DSA Basics Ready to level up your Data Structures and Algorithms (DSA) game? Doing DSA in Python isn’t just about getting the right output; it’s about writing clean, efficient, and Pythonic code. 🚀 If you’re still using 5 lines of code for a simple filter or a basic if-else block, it’s time for an upgrade. Today, I’m diving into two essential tools that make your algorithms sleeker: List Comprehensions and Ternary Operators. 1. List Comprehensions: The One-Liner Powerhouse Why write a for loop when you can generate a list in a single, readable line? It’s faster and keeps your workspace clutter-free. 2. Ternary Operators: Logic at a Glance When your algorithm needs a quick decision, ternary operators (conditional expressions) are your best friend. They are perfect for assigning values based on a condition without breaking the flow. The Syntax: value_if_true if condition else value_if_false 💡 Why this matters for DSA: Readability: Interviewers love code that is easy to follow. Efficiency: List comprehensions are often slightly faster than manual append() calls. Focus: It allows you to focus on the logic of the algorithm rather than the boilerplate of the syntax. What’s your favorite Python trick for competitive programming? Let’s discuss in the comments! 👇 #Python #DataStructures #Algorithms #Coding #SoftwareEngineering #Pythonic #ProgrammingTips
To view or add a comment, sign in
-
🐛 Most NumPy bugs are shape bugs. Why this matters: - Broadcasting, vectorization, and shapes — the 3 things that unlock speed and clarity. This topic appears repeatedly in interviews and real projects, so depth matters. Deep dive: - 📐 Always think in shapes first: • (n,) → 1D array • (n,1) → column vector • (n,d) → 2D matrix • Write them down while coding! | Practical note: connect this point to a real dataset, tool, or system decision. - ⚡ Vectorization beats Python loops every time: • Use matrix ops • Boolean masks • Aggregation functions (np.sum, np.mean) | Practical note: connect this point to a real dataset, tool, or system decision. - 📡 Broadcasting: dimensions of size 1 expand to match the other operand: • Powerful but easy to misuse • Understand the rules before relying on it | Practical note: connect this point to a real dataset, tool, or system decision. - 🔧 Use .reshape and keepdims=True intentionally to avoid accidental broadcasting. | Practical note: connect this point to a real dataset, tool, or system decision. - 🐞 Debug tip: • Print array.shape constantly • Use small toy arrays to validate logic before scaling | Practical note: connect this point to a real dataset, tool, or system decision. How to practice today: - Define one measurable objective and baseline before changing anything. - Implement one small experiment and log outcomes clearly. - Review failure cases and write 3 improvements for the next iteration. Common mistakes to avoid: - Skipping evaluation design and relying only on one metric. - Ignoring edge cases and production constraints (latency/cost/drift). - Not documenting assumptions, data limits, and trade-offs. Mini challenge: - Build a small proof-of-concept on "Python for ML" and publish your learning with metrics + trade-offs. 📌 If you want, I'll post a mini cheatsheet: reshape vs ravel vs squeeze. #python #numpy #machinelearning #datascience #coding
To view or add a comment, sign in
-
🚀 Day 3 – Building a Problem-Solving Mindset Still on the grind. Still showing up. Because consistency > motivation. 📌 Today’s Problem: Sum of N Natural Numbers Looks simple. But again… the goal is not just solving — it’s how efficiently you think. 🔹 Approaches Explored 1️⃣ Naive Approach → Basic step-by-step addition 2️⃣ Using Recursion → Function calling itself to solve smaller parts 3️⃣ Formula-Based Approach → Direct mathematical solution for maximum efficiency 💡 Key Takeaway Same problem. Three different approaches. Different levels of thinking. ✔️ Learned how multiple solutions exist for one problem ✔️ Understood that efficiency improves with better thinking 📈 Progress Update: From solving problems → to choosing the best approach Consistency is the real unlock 🔑 #Python #ProblemSolving #100DaysOfCode #Consistency #LearningJourney #DeveloperMindset
To view or add a comment, sign in
-
-
There’s a difference between writing code and cultivating craft. Most developers chase complexity—more lines, more time, more “features.” But the best engineers develop a deeper instinct: how to express an idea with precision. That’s why I love Python’s one-liners. Not because they’re “short.” But because they reveal something real: clarity can be compressed—when you truly understand what you’re writing. A few examples: swapping values without clutter, transforming lists with comprehension, flattening nested structures, counting with the right tool, using ternary logic thoughtfully. These aren’t tricks. They’re reminders that efficiency isn’t only performance—it’s cognitive performance. ✅ Write code that the next person can read. ✅ Write code that future-you will trust. ✅ Let brevity serve meaning, not ego. #Python #OneLiner #LakkiData #LearningSteps
To view or add a comment, sign in
-
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