Microsoft Interview Question for SDE-2s


Team: STB and MVO
Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream> 
using namespace std; 

class VendingMachine
{
	private:
		int iWaterPending_;
		int iCoffeePending_;
		int iTeaPending_;
		int iSoupPending_;

        int iMaxWaterCount_;
        int iMaxCoffeCount_;
        int iMaxTeaCount_;
        int iMaxSoupCount_;
	public:
        VendingMachine()
        {
            iWaterPending_ = iCoffeePending_ = iTeaPending_ = iSoupPending_ = 0;
            iMaxWaterCount_ = iMaxCoffeCount_ = iMaxTeaCount_ = iMaxSoupCount_ = 0;
        }

        VendingMachine(int iMaxWater, int iMaxCoffee, int iMaxTea, int iMaxSoup) :
            iMaxWaterCount_(iMaxWater), iMaxCoffeCount_(iMaxCoffee), iMaxTeaCount_(iMaxTea), 
            iMaxSoupCount_(iMaxSoup)
        {
        }
        
        void AddWater(int iCount)
        {
            if (iWaterPending_ + iCount > iMaxWaterCount_)
            {
                //! print error message
                return;
            }
            iWaterPending_ += iCount;
        }
        
        void AddCoffee(int iCount)
        {
            if (iCoffeePending_ + iCount > iMaxCoffeCount_)
            {
                //! print error message
                return;
            }
            iCoffeePending_ += iCount;
        }

        void AddTea(int iCount)
        {
            if (iTeaPending_ + iCount > iMaxTeaCount_)
            {
                //! print error message
                return;
            }
            iTeaPending_ += iCount;
        }

        void AddSoup(int iCount)
        {
            if (iSoupPending_ + iCount > iMaxSoupCount_)
            {
                //! print error message
                return;
            }
            iSoupPending_ += iCount;
        }   

        void GetCoffee()
        {
            if (iCoffeePending_ == 0)
            {
                // Print error message
                // Water comes out without coffee
                iWaterPending_ = iWaterPending_ - 1;
                return;
            }
            iCoffeePending_ = iCoffeePending_ - 1;
            iWaterPending_ = iWaterPending_ - 1;
        }
        //! Similar functions for GetSoup, GetTea and GetWater
};

- Pankaj Kumar October 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

onestopinterviewprep.blogspot.com/2014/03/vending-machine-problem-dynamic.html

- Wellwisher March 26, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More