Stacks and Queues

Categories: Code Problem / Data Structures and Algorithms / Recursion (32) Code Problem / Data Structures and Algorithms / Binary Search (30) Code Problem / Data Structures and Algorithms / Linked Lists (26) Code Problem / Data Structures and Algorithms / Backtracking (26) Code Problem / Data Structures and Algorithms / Stacks and Queues (25) Code Problem / Data Structures and Algorithms / Hashing (24) Databases / SQL / Backup and Recovery (19) Code Problem / Data Structures and Algorithms / Arrays and Strings (19) Databases / SQL / Database Normalization (18) Code Problem / Data Structures and Algorithms / Time Complexity (17) Databases / SQL / Locking (16) Databases / SQL / Replication (15) Databases / SQL / SQL Queries (13) Code Problem / Data Structures and Algorithms / Graphs (13) Databases / SQL / Database Security (13)  More...

Recent solutions:

Check these solutions from our community:
Code Problem / Data Structures and Algorithms DifficultyMedium JavaScript
Implement a stack with a max function that returns the maximum value in the stack.
About this solution: The candidate's solution is correct and uses a stack to store the values and a variable to store the max value. The max value is updated every time a new value is pushed to the stack. The max function returns the max value.
Nov 22
Code Problem / Data Structures and Algorithms DifficultyMedium JavaScript
Given a list of integers, this function will return the sum of the integers at the odd indices. For example, given the input list [1, 2, 3, 4, 5], the function will return 9.
About this solution: The candidate's solution is correct and efficient. The for loop is the most efficient way to iterate through an array, and the if statement is the most efficient way to check if an index is odd. The sum variable is the most efficient way to store the sum of the values at the odd indices. The return statement is the most efficient way to return the sum variable.
Nov 22
Code Problem / Data Structures and Algorithms DifficultyMedium C++
Given a stack of integers, this algorithm will sort the stack in ascending order. Additional stacks may be used to hold the elements, but no other data structures are allowed.
About this solution: The candidate's solution correctly sorts the stack in ascending order. The candidate uses an additional stack to hold the elements and correctly pops and pushes the elements in the correct order.
Nov 21
Code Problem / Data Structures and Algorithms DifficultyMedium Python
Given a list of integers, this function returns the sum of the integers in even-numbered positions in the list. For example, given the list [1, 2, 3, 4, 5], the function would return 6 (2 + 4).
About this solution: The candidate's solution correctly solves the problem and demonstrates a level of completeness. The approach is straightforward and easy to follow.
Nov 21
Code Problem / Data Structures and Algorithms DifficultyMedium Python
This technical problem asks the reader to write a function that returns the sum of the elements in a stack. An example input and output is provided.
About this solution: This solution correctly calculates the sum of the elements in a stack. However, it would be helpful if the function included a docstring explaining what it does. Additionally, it is generally good practice to include comments in code to explain what is happening. For example, a comment explaining why the sum variable is initialized to 0 would be helpful.
Nov 10
Code Problem / Data Structures and Algorithms DifficultyMedium Python
This problem asks for a efficient way to implement a Queue using two Stacks.
About this solution: The candidate's solution is correct and solves the problem. The candidate's approach is to use two stacks, with one stack being used for enqueueing and the other stack being used for dequeueing. This is a valid approach.
Nov 10
Code Problem / Data Structures and Algorithms DifficultyMedium Python
This problem is about finding the largest integer in a list. An example input would be [1, 2, 3, 4, 5], and the expected output would be 5.
About this solution: This solution correctly returns the largest integer in the list. However, it is not very robust - for example, if the list is empty, it will return an error. A more robust solution would check for an empty list and return a suitable default value (e.g. 0).
Nov 09
Code Problem / Data Structures and Algorithms DifficultyMedium Python
This technical problem asks you to implement a queue using two stacks. This can be done by pushing the elements onto one stack, and then popping them off onto the other stack when you need to dequeue an element.
About this solution: The candidate's solution correctly implements a queue using two stacks. The enqueue stack is used to push elements into the queue, and the dequeue stack is used to pop elements from the queue. The dequeue stack is only used when it is empty. When the dequeue stack is empty, the candidate correctly pops all the elements from the enqueue stack and pushes them into the dequeue stack. This way, the dequeue stack will always have the elements in the reverse order of the enqueue stack. This way, the first element pushed into the enqueue stack will be the first element to be popped from the dequeue stack. This way, the last element pushed into the enqueue stack will be the last element to be popped from the dequeue stack. In this way, the queue is correctly implemented using two stacks.
Nov 08