Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7943193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:11:48+00:00 2026-06-04T00:11:48+00:00

This is what I found on a algorithm/interview question book, someone else had several

  • 0

This is what I found on a algorithm/interview question book, someone else had several posts on this one, but I still have some questions, here is the original text and code in the book:

implement 3 stacks using 1 array:
Approach 2:

In this approach, any stack can grow as long as there is any free space in the array.
We sequentially allocate space to the stacks and we link new blocks to the previous block. This means any new element in a stack keeps a pointer to the previous top element of that particular stack.
In this implementation, we face a problem of unused space. For example, if a stack deletes some of its elements, the deleted elements may not necessarily appear at the end of the array. So, in that case, we would not be able to use those newly freed spaces.
To overcome this deficiency, we can maintain a free list and the whole array space would be given initially to the free list. For every insertion, we would delete an entry from the free list. In case of deletion, we would simply add the index of the free cell to the free list.
In this implementation we would be able to have flexibility in terms of variable space utilization but we would need to increase the space complexity.

1 int stackSize = 300;
2 int indexUsed = 0;
3 int[] stackPointer = {-1,-1,-1};
4 StackNode[] buffer = new StackNode[stackSize * 3];

5 void push(int stackNum, int value) {
6     int lastIndex = stackPointer[stackNum];
7     stackPointer[stackNum] = indexUsed;
8     indexUsed++;
9     buffer[stackPointer[stackNum]]=new StackNode(lastIndex,value);
10 }

11 int pop(int stackNum) {
12    int value = buffer[stackPointer[stackNum]].value;
13    int lastIndex = stackPointer[stackNum];
14    stackPointer[stackNum] = buffer[stackPointer[stackNum]].previous;
15    buffer[lastIndex] = null;
16    indexUsed--;
17    return value;
18 }

19 int peek(int stack) { return buffer[stackPointer[stack]].value; }

20 boolean isEmpty(int stackNum) { return stackPointer[stackNum] == -1; }
21
22 class StackNode {
23    public int previous;
24    public int value;
25    public StackNode(int p, int v){
26       value = v;
27       previous = p;
28    }
29 }

my question is: in the mothod pop(), after setting buffer[lastIndex] to null(line 15), then indexUsed–(line 16), will the indexUsed be the head of the empty space?
let’s call the Stack Top nodes of first stack : S1, second stack : S2, third stack : S3;
what happens if:
I have s1 followed by s2 and s3 pushed before I wanted to pop s1,

index:    10   11   12    13
-------------------------------
| ...   | s1 | s2 | s3 | null |
-------------------------------

the indexUsed is now 13.
Now if I want to do pop() on 1st stack,
it will become:

index:     10    11   12    13
----------------------------------
| ...   | null | s2 | s3 | null |
----------------------------------

and the indexUsed is now 13– , that is 12,
so what happends if i want to push a new Node onto this array,
according to the push() method, at line 7 the indexUsed is assign to the stackPointer and is used as pushing index onto the array, wouldn’t that overwrite the s3(top node of stack3) in the 12th entry?

added:

and how do I change it to make it work?
at first glance I would think about implementing an boolean[] to keep track of the usage of each entry in the storage array, but that would be time and space consuming.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:11:50+00:00Added an answer on June 4, 2026 at 12:11 am

    As far as I can tell, you are correct–it is not storing enough info to indicate holes in memory.

    One of the big advantages to a stack is that it can be allocated on top of an array-list instead of a Linked List saving the memory of the previous/next pointers–this implementation eliminates that advantage–it’s difficult to imagine an application where this was actually a good solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found this question in the one site which was about counting sort
I found this interview question, and I couldn't come up with an algorithm better
I found this question that is discussing what I would like to do, but
I am trying to draw a cylinder in OpenGl. I found this algorithm but
I found this algorithm here . I have a problem, I cant seem to
I need help in understanding this interview question: Q: Find an algorithm to find
So far, I have implemented the algorithm found on this blog post with limited
I found this on an interview questions site and have been pondering it for
What's the Hi/Lo algorithm? I've found this in the NHibernate documentation (it's one method
I was trying to solve random coding kata's and found this one, my question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.