Delete Mid Element from Stack Problem Statement

You are provided with a stack "ARR" containing "N+1" elements. Your task is to delete the middlemost element so that the resulting stack contains "N" elements.

Example:

Input:
ARR = [1, 2, 3, 4, 5], N = 4
Output:
ARR = [1, 2, 4, 5]
Explanation:

In the example above, the stack has an odd number of elements. Therefore, the middle element is the ((N+1)/2)th element, which is removed in the output.

Input:
ARR = [5, 6, 7, 8], N = 3
Output:
ARR = [5, 7, 8]
Explanation:

In this example, the stack has an even number of elements. Among the two middle elements, we remove the one which appears first. Thus, the middle element is the ((N+1)/2 - 1)th element, which is 6 and is removed in the output.

Input:

The first line contains an integer 'T' representing the number of test cases. For each test case, the first line contains an integer 'N', where 'N+1' indicates the number of elements in the stack initially. The second line contains 'N+1' space-separated integers representing the stack elements.

Output:

For each test case, print 'N' space-separated integers representing the stack elements after removing the middle element. Each test case's output should be printed on a separate line.

Constraints:

  • 1 <= T <= 100
  • 1 <= N+1 <= 3000
  • 0 <= data <= 109
  • Time limit: 1 second

Note: You do not need to print anything; the function should only return the required result.

AnswerBot
4mo

Delete the middle element from a stack to reduce its size by one.

  • Identify the middle element based on whether the stack size is odd or even

  • Remove the middle element from the stack

  • Adjust the stack size...read more

Help your peers!
Select
Add answer anonymously...

Amazon Software Developer Intern interview questions & answers

A Software Developer Intern was asked 3mo agoQ. What is the code to calculate the distance between two nodes in a binary tree?
A Software Developer Intern was asked 4mo agoQ. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
A Software Developer Intern was asked 4mo agoQ. For a given array, how would you count the number of inversions?

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 3mo agoQ1. What is the code to calculate the distance between two nodes in a binary tree?
A Software Developer Intern was asked 4mo agoQ2. For a given array, how would you count the number of inversions?
A Software Developer Intern was asked 4mo agoQ3. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
Amazon Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits