𝗗𝗮𝘆 𝟲𝟳/𝟭𝟬𝟬 — 𝗜𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁 𝗤𝘂𝗲𝘂𝗲 𝘂𝘀𝗶𝗻𝗴 𝗦𝘁𝗮𝗰𝗸𝘀 Day 67. Building data structures from scratch. Can you build a queue using only stacks? Yes. Should you? Probably not. But it teaches you something valuable. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟮𝟯𝟮: Implement Queue using Stacks (Easy) 𝗧𝗵𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲: Implement a FIFO queue using only LIFO stacks. Operations: push, pop, peek, empty. The problem? Stacks are last-in-first-out. Queues are first-in-first-out. Opposite behaviors. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: Two stacks: in and out. My solution keeps elements in reverse order in the in stack, so the oldest element is always on top. On push: Move all elements from in to out Push new element to in Move everything back from out to in Pop and peek? Just operate on in directly. Time: O(n) per push, O(1) for pop/peek 𝗪𝗵𝘆 𝗜𝘁 𝗠𝗮𝘁𝘁𝗲𝗿𝘀: Design problems force you to understand data structures deeply. Not just use them—build them. This isn't the most efficient approach, but it works. And understanding trade-offs is what separates good developers from great ones. 𝗖𝗼𝗱𝗲: https://lnkd.in/gcASkCjm 𝗗𝗮𝘆 𝟲𝟳/𝟭𝟬𝟬 ✅ 𝟲𝟳 𝗱𝗼𝘄𝗻. 𝟯𝟯 𝘁𝗼 𝗴𝗼. #100DaysOfCode #LeetCode #Queue #Stack #DataStructures #SystemDesign #Algorithms #CodingInterview #Programming #Java #DesignProblems

To view or add a comment, sign in

Explore content categories