EzDevInfo.com

stack interview questions

Top stack frequently asked interview questions

Why is Java Vector class considered obsolete or deprecated?

Why is Java Vector considered a legacy class, obsolete or deprecated?

Isn't its use valid when working with concurrency?

And if I don't want to manually synchronize objects and just want to use a thread-safe collection without needing to make fresh copies of the underlying array (as CopyOnWriteArrayList does), then is it fine to use Vector?

What about Stack, which is a subclass of Vector, what should I use instead of it?


Source: (StackOverflow)

Why do stacks typically grow downwards?

I know that in the architectures I'm personally familiar with (x86, 6502, etc), the stack typically grows downwards (i.e. every item pushed onto the stack results in a decremented SP, not an incremented one).

I'm wondering about the historical rationale for this. I know that in a unified address space, it's convenient to start the stack on the opposite end of the data segment (say) so there's only a problem if the two sides collide in the middle. But why does the stack traditionally get the top part? Especially given how this is the opposite of the "conceptual" model?

(And note that in the 6502 architecture, the stack also grows downwards, even though it is bounded to a single 256-byte page, and this direction choice seems arbitrary.)


Source: (StackOverflow)

Advertisements

Android: Clear Activity Stack

I'm having several activities in my application. and flow is very complicated. When I click the Logout application naviagates to login Screen and from there user can exit by cancel buton (calling system.exit(0) )

when I exit or back button, system invokes an activity from stack :( how can I clear all the activities in the stack when i reach Login screen? calling finish() is not practical as there are so many activities and some activities should no be closed when they are active such as native camera invoking activity.

validateuser logoutuser = new validateuser();
logoutuser.logOut();
Intent loginscreen = new Intent(homepage.this, Login2.class);
(homepage.this).finish();
loginscreen.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(loginscreen);

Source: (StackOverflow)

design a stack such that getMinimum( ) should be O(1)

This is one of an interview question. You need to design a stack which holds an integer value such that getMinimum() function should return the minimum element in the stack.

For example: consider the below example

case #1

5  --> TOP
1
4
6
2

When getMinimum() is called it should return 1, which is the minimum element 
in the stack. 

case #2

stack.pop()
stack.pop()

Note: Both 5 and 1 are poped out of the stack. So after this, the stack
looks like,

4  --> TOP
6
2

When getMinimum() is called is should return 2 which is the minimum in the 
stack.

Constriants:

  1. getMinimum should return the minimum value in O(1)
  2. Space constraint also has to be considered while designing it and if you use extra space, it should be of constant space.

Source: (StackOverflow)

How do you extract local variable information (address and type) from a Delphi program or the compiler-generated debug info?

My goal is:

  • Given a suspended thread in a Delphi-compiled 32 or 64-bit Windows program, to walk the stack (doable)
  • Given stack entries, to enumerate the local variables in each method and their values. That is, at the very least, find their address and type (integer32/64/signed/unsigned, string, float, record, class...) the combination of which can be used to find their value.

The first is fine and it's the second that this question is about. At a high level, how do you enumerate local variables given a stack entry in Delphi?


At a low level, this is what I've been investigating:

RTTI: does not list this kind of information about methods. This was not something I actually ever thought was a realistic option, but listing here anyway.

Debug information: Loading the debug info produced for a debug build.

  • Map files: even a detailed map file (a text-format file! Open one and have a look) does not contain local variable info. It's basically a list of addresses and source file line numbers. Great for address to file&line correlation, e.g. the blue dots in the gutter; not great for more detailed information
  • Remote debugging information (RSM file) - no known information on its contents or format.
  • TD32/TDS files: my current line of research. They contain global and local symbols among a lot of other information.

The problems I'm encountering here are:

  • There's no documentation of the TD32 file format (that I can find.)
  • Most of my knowledge of them comes from the Jedi JCL code using them (JclTD32.pas) and I'm not sure how to use that code, or whether the structures there are extensive enough to show local vars. I'm pretty certain it will handle global symbols, but I'm very uncertain about local. There are a wide variety of constants defined and without documentation for the format, to read what they mean, I'm left guessing. However, those constants and their names must come from somewhere.
  • Source I can find using TDS info does not load or handle local symbols.

If this is the right approach, then this question becomes 'Is there documentation for the TDS/TD32 file format, and are there any code samples that load local variables?'

A code sample isn't essential but could be very useful, even if it's very minimal.


Source: (StackOverflow)

Why not use pointers for everything in C++?

Suppose that I define some class:

class Pixel {
    public:
      Pixel(){ x=0; y=0;};
      int x;
      int y;
}

Then write some code using it. Why would I do the following?

Pixel p;
p.x = 2;
p.y = 5;

Coming from a Java world I always write:

Pixel* p = new Pixel();
p->x = 2;
p->y = 5;

They basically do the same thing, right? One is on the stack while the other is on the heap, so I'll have to delete it later on. Is there any fundamental difference between the two? Why should I prefer one over the other?


Source: (StackOverflow)

Do threads have a distinct heap?

As far as I know each thread gets a distinct stack when the thread is created by the operating system. I wonder if each thread has a heap distinct to itself also?


Source: (StackOverflow)

Implement Stack using Two Queues

A similiar question was asked earlier there, but the question here is the reverse of it, using two queues as a stack. The question...

Given two queues with their standard operations (enqueue, dequeue, isempty, size), implement a stack with its standard operations (pop, push, isempty, size).

There should be TWO versions of the solution.

  • Version A: The stack should be efficient when pushing an item.
  • Version B: The stack should be efficient when popping an item.

I am interested in the algorithm more than any specific language implementations. However, I welcome solutions expressed in languages which I am familiar (Java, C#, Python, VB, JavaScript, PHP).


Source: (StackOverflow)

Stack, Static, and Heap in C++

I've searched, but I've not understood very well these three concepts. When do I have to use dynamic allocation (in the heap) and what's its real advantage? What are the problems of static and stack? Could I write an entire application without allocating variables in the heap?

I heard that others languages incorporate a "garbage collector" so you don't have to worry about memory. What does the garbage collector do?

What could you do manipulating the memory by yourself that you couldn't do using this garbage collector?

Once someone said to me that with this declaration:

int * asafe=new int;

I have a "pointer to a pointer". What does it mean? It is different of:

asafe=new int;

?


Source: (StackOverflow)

Android: Clear the back stack

In Android I have some activities, let's say A, B, C.

In A I use this code to open B:

Intent intent = new Intent(this, B.class);
startActivity(intent);

In B I use this code to open C:

Intent intent = new Intent(this, C.class);
startActivity(intent);

When the user taps a button in C I want to go back to A and clear the back stack (close both B and C). So when the user use the back button B and C will not show up, I've been trying the following:

Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent);

But B and C are still showing up if I use the back button when I'm back in activity A. How can I avoid this?


Source: (StackOverflow)

What is stack unwinding?

What is stack unwinding? Searched through but couldn't find enlightening answer!


Source: (StackOverflow)

In C, do braces act as a stack frame?

If I create a variable within a new set of curly braces, is that variable popped off the stack on the closing brace, or does it hang out until the end of the function? For example:

void foo() {
   int c[100];
   {
       int d[200];
   }
   //code that takes a while
   return;
}

Will d be taking up memory during the code that takes a while section?


Source: (StackOverflow)

How to implement a queue using two stacks?

Suppose we have two stacks and no other temporary variable.

Is to possible to "construct" a queue data structure using only the two stacks?


Source: (StackOverflow)

Why is the use of alloca() not considered good practice?

alloca() allocates memory from Stack rather than heap which is case in malloc(). So, when I return from the routine the memory is freed. So, actually this solves my problem of freeing up of dynamically allocated memory. Freeing of memory allocated through malloc() is a major headache and if somehow missed leads to all sorts memory problems.

Why is the use of alloca() discouraged in spite of the above features?


Source: (StackOverflow)

How do you implement a Stack and a Queue in JavaScript?

What is the best way to implement a Stack and a Queue in JavaScript?

I'm looking to do the shunting-yard algorithm and I'm going to need these data-structures.


Source: (StackOverflow)