Programmer interview questions 100 (02)-design a stack containing min functions [data structure]

Source: Internet
Author: User

Question: To define the stack data structure, add a min function to obtain the minimum element of the stack. The time complexity of the min, push, and pop functions is O (1 ).

Analysis: this is an interview question from Google last year.

When I see this question, the first reaction is to sort all reverse elements in the stack every time a new element is pushed. In this way, the top element of the stack will be the smallest element. However, the data structure designed in this way is no longer a stack because it cannot guarantee that the elements pushed to the stack are first-in-first-out.

Add a member variable to the stack to store the smallest element (or the location of the smallest element ). Each time a new element is pushed to the stack, if the element is smaller than the current minimum element, the minimum element is updated.

At first glance, this is a good idea. But if you think about it, there is an important question: if the current minimum element is pop out, how can we get the next minimum element?

Therefore, it is not enough to add only one member variable to store the smallest element (or the location of the smallest element. We need an auxiliary stack. Each time a new element is pushed, the smallest element (or the position of the smallest element) is also pushed. Considering that the type of the stack element may be a complex data structure, using the location of the smallest element can reduce space consumption) push to the auxiliary stack; each time an element pops out of the stack, pop auxiliary stack at the same time.

Reference code:

# Include <deque>
# Include <assert. h>

Template <typename T> class cstackwithmin
{
Public:
Cstackwithmin (void ){}
Virtual ~ Cstackwithmin (void ){}

T & Top (void );
Const T & Top (void) const;

Void push (const T & value );
Void POP (void );

Const T & min (void) const;

PRIVATE:
T> m_data; // The elements of stack
Size_t> m_minindex; // The indices of minimum elements
};

// Get the last element of mutable Stack
Template <typename T> T & cstackwithmin <t>: Top ()
{
Return m_data.back ();
}

// Get the last element of non-mutable Stack
Template <typename T> const T &
Cstackwithmin <t>: Top () const
{
Return m_data.back ();
}

// Insert an elment at the end of stack
Template <typename T> void cstackwithmin <t>: Push (const T &
Value)
{
// Append the data into the end of m_data
M_data.push_back (value );

// Set the index of minimum elment in m_data at the end of m_minindex
If (m_minindex.size () = 0)
M_minindex.push_back (0 );
Else
{
If (value <m_data [m_minindex.back ()])
M_minindex.push_back (m_data.size ()-1 );
Else
M_minindex.push_back (m_minindex.back ());
}
}

// Erases the element at the end of stack
Template <typename T> void cstackwithmin <t>: Pop ()
{
// Pop m_data
M_data.pop_back ();

// Pop m_minindex
M_minindex.pop_back ();
}

// Get the minimum element of stack
Template <typename T> const T &
Cstackwithmin <t>: min () const
{
Assert (m_data.size ()> 0 );
Assert (m_minindex.size ()> 0 );

Return m_data [m_minindex.back ()];
}

The following is an example of how to run the above Code:

Step: data stack auxiliary stack minimum value
1. Push 3 3 0 3
2. Push 4 3, 4, 0, 0 3
3. Push 2 3, 4, 2, 0, 2 2
4. Push 1 3,4
5. POP 3, 4, 2, 0, 2 2
6. POP 3, 4, 0 3
7. Push 0 3, 4, 0 0, 0, 2 0

Discussion: if the idea is correct, writing the above Code is not very difficult. However, if you can pay attention to some details, you will undoubtedly be able to score points during the interview. For example, I did the following work in the above Code:

· Use a template class. If the element type of others is only int, the template will give the interviewer a good impression;

· Two versions of top functions. In many classes, you must provide access functions for members of the const and non-const versions;

· Assert in the min function. Every software company requires programmers to write code securely;

· Add comments. Comments not only improve the readability of the code, but also increase the amount of code. Why not?

In short, if the time permits during the interview, try to write the code beautifully. Maybe a few highlights in the Code will make it easy for you to get your favorite offer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.