Amazon Interview Question
SDE-3sCountry: 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))
package com.ajay;
import java.util.ArrayList;
import java.util.HashSet;
public class CardSequence {
private static int max = 0;
public static void main(String[] args) {
char [][] cards = {{'H','3'}, {'H','4'}, {'S','2'},{'D','5'}, {'D','1'}};
HashSet visited = new HashSet();
int max = 0;
int count[] = new int[cards.length];
for(int i = 0; i < cards.length; i++) {
char[] currCard = cards[i];
visited.add(currCard);
helper(cards, currCard, visited, count, i);
max = Math.max(max, count[i]);
}
System.out.println(max);
}
private static void helper(char[][] cards,char[] currCard, HashSet visited, int[] count, int cIndex) {
ArrayList children = getLinks(cards, currCard);
if(children.size() > 0) {
count[cIndex] = count[cIndex]+1;
}
for(int i = 0; i < children.size(); i++) {
char[] tcard = (char[]) children.get(i);
if(!visited.contains(tcard)) {
visited.add(tcard);
helper(cards, tcard, visited,count,cIndex);
}
}
}
private static String getCardName(char[] card) {
return card[0]+""+card[1];
}
private static ArrayList getLinks(char[][] cards,char[] currCard) {
ArrayList list = new ArrayList();
for(char[] card:cards) {
if(card != currCard) {
if(card[0] == currCard[0] || card[1] == currCard[1]) {
list.add(card);
}
}
}
return list;
}
}
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