## Amazon Interview Question

SDE-3s**Country:**United States

**Interview Type:**Phone Interview

```
let pairs = [["S", 3], ["H", 4], ["S", 4], ["D", 5], ["D", 1],["S",5]]
function findPair(pair, index, sequence) {
let suit = pair[0]
let snum = pair[1]
for (let n = 0; n < pairs.length; n++) {
if (n == index) continue
const ipair = pairs[n];
let isuit = ipair[0]
let isnum = ipair[1]
if (sequence[isuit + "_" + isnum]) continue
if (suit == isuit) {
sequence[isuit + "_" + isnum] = true
findPair(ipair, n, sequence)
}
if (snum == isnum) {
sequence[isuit + "_" + isnum] = true
findPair(ipair, n, sequence)
}
}
return sequence
}
const element = pairs[0];
let sequence = {}
sequence[element[0] + "_" + element[1]] = true
let s= findPair(element, 0, sequence)
console.log(Object.keys(s).length)
```

public int maxCardSelection(Object[][] deck) {

int ms = 0;//max selections

/*loop through the card deck*/

for (int i = 0; i < deck.length; i++) {

if (i == 0) {

//just pick the card

ms += 1;

} else {

/*check if the suite or number equals previous card, this is allowed*/

if ((deck[i][0].equals(deck[i - 1][0])) || (deck[i][1].equals(deck[i - 1][1]))) {

ms += 1;

} else {

/*we cannot continue*/

return ms;

}

}

}

return ms;

}

def deckOfCards(cards, pair): # sde 3 amazon

result = []

result.append(pair)

for i in cards:

if i == pair:

continue

pair = result[-1] # get the last item of the list

if i[0] == pair[0] or i[1] == pair[1]:

result.append(i)

return result

cards = [["H", 3], ["H", 4], ["S", 4], ["D", 5], ["D", 1]]

pair = ["H",3]

print(deckOfCards(cards,pair))

{{

def deckOfCards(cards, pair): # sde 3 amazon

result = []

result.append(pair)

for i in cards:

if i == pair:

continue

pair = result[-1] # get the last item of the list

if i[0] == pair[0] or i[1] == pair[1]:

result.append(i)

return result

cards = [["H", 3], ["H", 4], ["S", 4], ["D", 5], ["D", 1]]

pair = ["H",3]

print(deckOfCards(cards,pair))

}}

def deckOfCards(cards, pair): # sde 3 amazon

result = []

result.append(pair)

for i in cards:

if i == pair:

continue

pair = result[-1] # get the last item of the list

if i[0] == pair[0] or i[1] == pair[1]:

result.append(i)

return result

cards = [["H", 3], ["H", 4], ["S", 4], ["D", 5], ["D", 1]]

pair = ["H",3]

print(deckOfCards(cards,pair))

I think we can solve this problem with the same logic used for longest increasing subsequence. Consider the cards as an array and for each of the other cards see if we can add the current card to our hand. We can add it to our hand if the suit is the same or the value of the card is the same, compare the longest sequence of the current card to the longest sequence of the other card + 1.

- Ricky Betson December 28, 2020