Amazon Interview Question


Country: United States




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

It can be implemented by using the decorator design pattern.

- Gul Ershad December 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It can be implemented by using decorator design pattern.

- mailtogulershad December 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How the hell it is a decorator ? Explain.. coz this is a classic example of Command design pattern.

Reason is it is command coz you can configure what slots goes to what denomination of currency and how many denomination can be arranged when we set the ATM machine object.

This also, employs State design pattern, coz every time, you do the operation, you will change the state of machine.

Can share the code if required but will be messy in such question coz its purely a discussion based.

- hprem991 December 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this can be solved by multiple patterns. We just need to find out which one is better. I tried with few of below:
1) State pattern (To check which States such as Card validation, Pin validation, operation, printing etc)
2) Decorator pattern (We can perform multiple add ons on Account objects such as Deposit, Withdrawl, transfer etc..)
3) Chain of responsibility (For withdrawal operation where current notes needs to be calculated base don amount withdrawn..)

- TechPrep January 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be solved using State Pattern
Demo: http://ideone.com/zg3uxV

public interface AtmMachineState {
     void insertDebitCard();
     void ejectDebitCard();
     void enterPinAndWithdrawMoney();
}

public class NoDebitCardState implements AtmMachineState{

    @Override
    public void insertDebitCard() {
        System.out.println("Debit Card is inserted");
    }
    @Override
    public void ejectDebitCard() {
        System.out.println("No Debit Card in the slot, so you can't eject card");
    }
    @Override
    public void enterPinAndWithdrawMoney() {
        System.out.println("No Debit Card in ATM, so you can't enter pin and password");
    }
}

public class HasDebitCardState implements AtmMachineState{
    @Override
    public void insertDebitCard() {
        System.out.println("Debit Card is already in the ATM, so you can't insert another");
    }

    @Override
    public void ejectDebitCard() {
        System.out.println("Debit card is ejected");
    }

    @Override
    public void enterPinAndWithdrawMoney() {
        System.out.println("Pin Card entered correctly, and money has been withdrawn");
    }
}

public class AtmMachine implements AtmMachineState {
    private AtmMachineState atmMachineState;
    public AtmMachine(){
        atmMachineState = new NoDebitCardState();
    }

    public AtmMachineState getAtmMachineState() {
        return atmMachineState;
    }

    public void setAtmMachineState(AtmMachineState atmMachineState) {
        this.atmMachineState = atmMachineState;
    }

    @Override
    public void insertDebitCard() {
        atmMachineState.insertDebitCard();
    }

    @Override
    public void ejectDebitCard() {
        atmMachineState.ejectDebitCard();
        if(atmMachineState instanceof HasDebitCardState){
            AtmMachineState noDebitCardState = new NoDebitCardState();
            setAtmMachineState(noDebitCardState);
            System.out.println("ATM Machine internal state has been moved to"+atmMachineState.getClass().getName());
        }
    }

    @Override
    public void enterPinAndWithdrawMoney() {
        atmMachineState.enterPinAndWithdrawMoney();
    }
}

public class Client {
    public static void main(String[] args) {
        AtmMachine atmMachine = new AtmMachine();
        System.out.println("ATM Machine Current state"+ atmMachine.getClass().getName());
        System.out.println();
        atmMachine.enterPinAndWithdrawMoney();
        atmMachine.ejectDebitCard();
        atmMachine.insertDebitCard();
        System.out.println("\n ---------------");
        System.out.println("ATM Machine Current State"+ atmMachine.getClass().getName());
        System.out.println();
        atmMachine.enterPinAndWithdrawMoney();
        atmMachine.insertDebitCard();
        atmMachine.ejectDebitCard();

    }
}

- neelabhsingh January 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I may be wrong but - as far as I know:
Amazon will never be interested in any design pattern. The refer "System Design" which is not Design Pattern.

- Arnab Dutta February 16, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public enum TransactionType
    {
        Debit,
        Credit,
        CheckBalance
    }

    public class ATMMachine
    {
        private int ReadCard()
        {
            return 123456;
        }

        public bool Verify(int pin)
        {
            return Bank.GetBankInstance().Verify(ReadCard(), pin);
        }

        public object SelectTransactionType(TransactionType type)
        {
            switch (type)
            {
                case TransactionType.Debit:
                    return new Debit(ReadCard());
                case TransactionType.Credit:
                    return new Credit(ReadCard());
                default:
                    return new BalanceCheck(ReadCard());
            }
        }
    }

    public class Debit 
    {
        private double debitAmount;
        private int accountId;

        public Debit(int id)
        {
            accountId = id;
        }

        public void InsertDebitAmount(double amount)
        {
            // here can be validation of monies
            if (amount <= 0)
                throw new Exception("Amount cannot less than or equal to zero");
            debitAmount = amount;
        }

        public void DebitToAccount()
        {
            Bank.GetBankInstance().GetAccountForId(accountId).Debit(debitAmount);
        }

    }

    public class Credit
    {
        private int creditAmount;
        private int accountId;

        public Credit(int id)
        {
            accountId = id;
        }

        public void EnterCreditAmount(int amount)
        {
            // here can be validation of monies
            if (amount <= 0)
                throw new Exception("Amount cannot less than or equal to zero");
            if (amount % 20 != 0)
                throw new Exception("Please enter amount in mulitples of 20");

            creditAmount = amount;
        }

        public int CreditToAccount()
        {
           return Bank.GetBankInstance().GetAccountForId(accountId).Credit(creditAmount);
        }
    }

    public class BalanceCheck
    {
        private int accountId;

        public BalanceCheck(int id)
        {
            accountId = id;
        }

        public double CheckBalance()
        {
            return Bank.GetBankInstance().GetAccountForId(accountId).GetBalance();
        }
    }

    public class Account
    {
        private int accoutNumber;
        private int pin;

        private double balance;

        public int GetPin()
        {
            return pin;
        }

        public void UpdatePin(int p)
        {
            pin = p;
        }

        public double GetBalance()
        {
            return balance;
        }

        public void Debit(double amount)
        {
            balance += amount;
        }

        public int Credit(int amount)
        {
            if (amount < balance)
            {
                balance -= amount;
                return amount;
            }
            else
                throw new Exception("Not enough balance");
        }
    }

    public class Bank
    {
        private Bank _instance;
        Dictionary<int, Account> accountMap;
        private Bank()
        {
            accountMap = new Dictionary<int, Account>();
        }

        public static Bank GetBankInstance()
        {
            if (_instance == null)
                _instance = new Bank();
            return _instance;
        }

        public void AddAccount(Account account, int id)
        {
            accountMap.Add(id, account);
        }
        public bool Verify(int id, int pin)
        {
            if (accountMap.ContainsKey(id))
            {
                return accountMap[id].GetPin() == pin;
            }
            return false;
        }

        public Account GetAccountForId(int id)
        {
            return accountMap[id];
        }

}

- Anonimouse February 17, 2017 | 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