## Amazon Interview Question for SDE-3s

Country: United States
Interview Type: Phone Interview

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

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.

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

can we do like Insertion sort algorithm implementation

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

We can relate this problem to a strongly connected component in the graph.
Count maximum of all the strongly connected component nodes which are connected to each other.

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

aster

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

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

``````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)``````

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

const element = pairs[0];
let s= findPair(element, 0, sequence)

How about the case when first element is not part of series?

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

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;
}

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

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))

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

{{
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))
}}

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

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))

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

step 1 : create a undirected graph with edges between cards having same suit or number.
Step 2: do dFS on each vertex and save max .
Step 3: return max value of max

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

``````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];
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) {

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)) {
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]) {
}
}
}
return list;
}
}``````

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.

### 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.