LeetCampus
Interview Question

What is the difference between a stack and a queue, and can you provide an example of when to use each structure?

November 27, 2025
0 views
Difficulty: Medium
Popularity: Moderate
Share on

Question Explanation

This question is commonly asked in technical interviews to assess a candidate's understanding of data structures, a foundational concept in computer science. Interviewers look for clarity in explaining the differences between these structures, as well as the ability to apply them in real-world scenarios. A stack follows a Last In, First Out (LIFO) principle, meaning the last element added is the first one to be removed. It's like a stack of plates; you add to the top and remove from the top. On the other hand, a queue operates on a First In, First Out (FIFO) basis, where the first element added is the first to be removed, similar to people waiting in line. Candidates often misunderstand the nuances of when to use each structure; for example, stacks are great for situations requiring backtracking (like undo operations), while queues are perfect for scenarios like process scheduling. Understanding these principles helps candidates apply the correct structure in programming tasks, thereby optimizing performance and resource management.

Sample Answers

Example 1: College Project - Managing Tasks

During my final year project, my team developed a task management application. We used a stack to implement the 'undo' feature, allowing users to revert their last action. Each time a user performed an action, we pushed it onto the stack. If they decided to undo, we popped the last action from the stack. This made the application intuitive and efficient. Conversely, we used a queue to manage tasks submitted by users. When a user submitted a new task, it was added to the end of the queue, and we processed tasks in the order they were received. This setup mirrored real-life task management, ensuring that all tasks were addressed fairly and sequentially.

Example 2: Part-time Job - Customer Service

In my part-time job at a retail store, we often faced busy periods where customers lined up to check out. We used a queue system at the registers to ensure that customers were served in the order they arrived. This approach minimized confusion and made the checkout process smoother. Additionally, I volunteered at a local community center where we organized events. For our event registration, we implemented a stack system to manage last-minute registrations. When participants arrived, they would sign up, and if we reached capacity, we could quickly handle late registrations by processing the latest sign-ups first, ensuring we could accommodate as many attendees as possible while managing space effectively.

Example 3: First Job Experience - Software Development

In my first job as a junior software developer, I worked on a project that required processing a series of tasks submitted by users. We used a queue to manage these tasks, ensuring that they were processed in the order they were received. This was crucial for maintaining system integrity and user satisfaction. At the same time, we utilized a stack for maintaining user session history. When users navigated through the application, we pushed each page onto the stack. If they needed to go back, we popped from the stack to retrieve the last visited page. This dual use of both data structures allowed for a seamless user experience and efficient task management.

Keywords

stack vs queuedata structuresLIFOFIFOprogramming interview

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions