Interview Question for SDE-2s


Country: India
Interview Type: Written Test




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

Assuming the card array is not already sorted..

def buy_cards(cards, cash):
    if cards:
        cards = sorted(cards)
        cards = [0] + cards + [cards[-1] + cash]
    res = 0
    for n,m in zip(cards, cards[1:]):
        if not cash:
            break
        d = int((-2*n - 3 + ((2*n+3)**2 + 8*(cash-n-1))**0.5)//2)
        if d >= 0:
            d = min(d, m-n-2)
            res += d+1
            cost = (d*d + d*(2*n+3) + 2*(n+1))//2
            cash -= cost
    return res

print(buy_cards([2,3,5], 7))
print(buy_cards([2,3,5,11], 50))

Output:

2
7

- adr September 21, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Won't a greedy solution suffice ? You iterate from 1 to the amount D , and for each number you check whether your sister already has it, if she does not , you check whether you can buy the card ( you maintain the amount you spend ) , if yes then add 1 to the answer.

- lakhansoren March 23, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MaximumCardsToBuy {


    static int buyMaximumCards(List<Integer> sisCards, int amount) {
        if (amount == 0) {
            return 0;
        }
        int result = 0;
        int card = 1;
        while (amount > 0 && card <= amount) {
            if (!sisCards.contains(card) && amount >= card) {
                result++;
                amount -= card;
            }
            card++;
        }
        return result;
    }

    public static void main(String[] args) {
        List<Integer> sisCards = Arrays.asList(2, 3, 5);
        int amount = 7;
        System.out.println(buyMaximumCards(sisCards, amount));

        sisCards = Arrays.asList();
        amount = 10;
        System.out.println(buyMaximumCards(sisCards, amount));

        sisCards = Arrays.asList(2, 3, 5, 10);
        amount = 17;
        System.out.println(buyMaximumCards(sisCards, amount));
    }
}

- Som September 29, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MaximumCardsToBuy {


    static int buyMaximumCards(List<Integer> sisCards, int amount) {
        if (amount == 0) {
            return 0;
        }
        int result = 0;
        int card = 1;
        while (amount > 0 && card <= amount) {
            if (!sisCards.contains(card) && amount >= card) {
                result++;
                amount -= card;
            }
            card++;
        }
        return result;
    }

    public static void main(String[] args) {
        List<Integer> sisCards = Arrays.asList(2, 3, 5);
        int amount = 7;
        System.out.println(buyMaximumCards(sisCards, amount));

        sisCards = Arrays.asList();
        amount = 10;
        System.out.println(buyMaximumCards(sisCards, amount));

        sisCards = Arrays.asList(2, 3, 5, 10);
        amount = 17;
        System.out.println(buyMaximumCards(sisCards, amount));
    }
}

- Anonymous September 29, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int buyMaximumCards(List<Integer> sisCards, int amount) {
        if (amount == 0) {
            return 0;
        }
        int result = 0;
        int card = 1;
        while (amount > 0 && card <= amount) {
            if (!sisCards.contains(card) && amount >= card) {
                result++;
                amount -= card;
            }
            card++;
        }
        return result;
    }

- Somendra Raj September 29, 2020 | 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