Skip to content

Commit bc52db6

Browse files
author
Antesh Sharma
committed
Added solutions for card deck and two team football match score problem
1 parent 54b1efe commit bc52db6

File tree

3 files changed

+332
-0
lines changed

3 files changed

+332
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
Coding Round 3 - at Nvidia
3+
* // 1. check all 52 cards available or not
4+
// 2. check which card is missing
5+
// 3. check if we have a duplicate card
6+
7+
8+
String[] cardsDeck = new String[]
9+
{ "AH", "KH", "10D", "5H", "AC", "10S", "AS", "8H", "9D", "5S", "3D", "7S", "2D", "JH",
10+
"QS", "2H", "QD", "6S", "5D", "8D", "2C", "JC", "KS", "KD", "4H", "3H", "3C", "6D", "QH", "9H", "JD", "7D",
11+
"AD", "2S", "3S", "4S", "9S", "9C", "5C", "7C", "QC", "10H", "10C", "8S", "JS", "4D", "6H", "4C", "8C",
12+
"7H", "6C", "KC" };
13+
14+
Cards: A J Q K, 2 - 10
15+
Suit
16+
D - Diamond
17+
S - Spade
18+
H - Heart
19+
C - Club
20+
*
21+
*
22+
*
23+
*
24+
* */
25+
26+
import java.util.*;
27+
import java.util.stream.Collectors;
28+
29+
public class CardDeckProblem {
30+
public static void main(String[] args) {
31+
32+
Set<String> prePopulatedCardDeck = prepopulateUniqueCards();
33+
34+
testNullCardDeck();
35+
test52CardsWithEmptyCardDeck();
36+
testDuplicateAndMissingCards(prePopulatedCardDeck);
37+
testMissingCards(prePopulatedCardDeck);
38+
testNoMissingCards(prePopulatedCardDeck);
39+
40+
}
41+
42+
43+
public static void testNullCardDeck() {
44+
validateCardDeck(null);
45+
}
46+
47+
public static void test52CardsWithEmptyCardDeck() {
48+
String[] cardsDeck = new String[]{"AH"};
49+
validateCardDeck(cardsDeck);
50+
}
51+
52+
public static void testDuplicateAndMissingCards(Set<String> prePopulatedCardDeck) {
53+
//duplicate AH
54+
String[] cardsDeck = new String[]
55+
{"AH", "AH", "10D", "5H", "AC", "10S", "AS", "8H", "9D", "5S", "3D", "7S", "2D", "JH",
56+
"QS", "2H", "QD", "6S", "5D", "8D", "2C", "JC", "KS", "KD", "4H", "3H", "3C", "6D", "QH", "9H", "JD", "7D",
57+
"AD", "2S", "3S", "4S", "9S", "9C", "5C", "7C", "QC", "10H", "10C", "8S", "JS", "4D", "6H", "4C", "8C",
58+
"7H", "6C", "KC"};
59+
Set<String> uniqueCardDeck = getUniqueCardDeck(cardsDeck);
60+
findMissingCards(prePopulatedCardDeck, uniqueCardDeck);
61+
}
62+
63+
public static void testNoMissingCards(Set<String> prePopulatedCardDeck) {
64+
//All valid 52 cards
65+
String[] cardsDeck = new String[]
66+
{"AH", "KH", "10D", "5H", "AC", "10S", "AS", "8H", "9D", "5S", "3D", "7S", "2D", "JH",
67+
"QS", "2H", "QD", "6S", "5D", "8D", "2C", "JC", "KS", "KD", "4H", "3H", "3C", "6D", "QH", "9H", "JD", "7D",
68+
"AD", "2S", "3S", "4S", "9S", "9C", "5C", "7C", "QC", "10H", "10C", "8S", "JS", "4D", "6H", "4C", "8C",
69+
"7H", "6C", "KC"};
70+
Set<String> uniqueCardDeck = getUniqueCardDeck(cardsDeck);
71+
findMissingCards(prePopulatedCardDeck, uniqueCardDeck);
72+
}
73+
74+
public static void testMissingCards(Set<String> prePopulatedCardDeck) {
75+
//missing AK, AH
76+
String[] cardsDeck = new String[]
77+
{"ZK", "ZK", "10D", "5H", "AC", "10S", "AS", "8H", "9D", "5S", "3D", "7S", "2D", "JH",
78+
"QS", "2H", "QD", "6S", "5D", "8D", "2C", "JC", "KS", "KD", "4H", "3H", "3C", "6D", "QH", "9H", "JD", "7D",
79+
"AD", "2S", "3S", "4S", "9S", "9C", "5C", "7C", "QC", "10H", "10C", "8S", "JS", "4D", "6H", "4C", "8C",
80+
"7H", "6C", "KC"};
81+
Set<String> uniqueCardDeck = getUniqueCardDeck(cardsDeck);
82+
findMissingCards(prePopulatedCardDeck, uniqueCardDeck);
83+
}
84+
85+
private static void validateCardDeck(String[] cardsDeck) {
86+
if (cardsDeck == null || cardsDeck.length == 0) {
87+
System.out.println("Invalid input or empty input");
88+
return;
89+
}
90+
91+
if (cardsDeck.length < 52) {
92+
System.out.println("Number of cards in the deck are less than 52");
93+
}
94+
}
95+
96+
private static Set<String> getUniqueCardDeck(String[] cardsDeck) {
97+
Set<String> uniqueCardDeck = new TreeSet<>();
98+
StringBuilder duplicate = new StringBuilder();
99+
for (String card : cardsDeck) {
100+
boolean result = uniqueCardDeck.add(card);
101+
if (!result) {
102+
duplicate.append(card + " ");
103+
}
104+
}
105+
String duplicateMessage = duplicate.toString().length() > 0 ? "Found duplicate card: " + duplicate.toString() : "No duplicate card found";
106+
System.out.println(duplicateMessage);
107+
return uniqueCardDeck;
108+
}
109+
110+
private static void findMissingCards(Set<String> prePopulatedCardDeck, Set<String> uniqueCardDeck) {
111+
Set<String> missingCards = prePopulatedCardDeck.stream().filter(prePopulatedCard -> {
112+
return uniqueCardDeck.stream().noneMatch(givenCard -> {
113+
return prePopulatedCard.equals(givenCard);
114+
});
115+
}).collect(Collectors.toSet());
116+
117+
String missingCardMessage = missingCards.size() > 0 ? "Missing cards: " + missingCards : "No missing card";
118+
System.out.println(missingCardMessage);
119+
}
120+
121+
122+
private static Set<String> prepopulateUniqueCards() {
123+
124+
Set<String> uniqueCardDeck = new TreeSet<>();
125+
126+
for (int i = 0; i < 4; i++) {
127+
for (int j = 1; j <= 13; j++) {
128+
StringBuilder sb = new StringBuilder();
129+
if (j == 1) {
130+
sb.append("A");
131+
} else if (j == 11) {
132+
sb.append("J");
133+
} else if (j == 12) {
134+
sb.append("Q");
135+
} else if (j == 13) {
136+
sb.append("K");
137+
} else {
138+
sb.append(j);
139+
}
140+
141+
if (i == 0) {
142+
sb.append("D");
143+
} else if (i == 1) {
144+
sb.append("S");
145+
} else if (i == 2) {
146+
sb.append("H");
147+
} else if (i == 3) {
148+
sb.append("C");
149+
}
150+
uniqueCardDeck.add(sb.toString());
151+
}
152+
}
153+
154+
return uniqueCardDeck;
155+
156+
}
157+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#HackerRank coding round interview asked at Nvidia
2+
##Football Scores
3+
The number of goals achieved by two football teams in matches in a league is given in the form of two
4+
lists. Consider:
5+
6+
Football team A, has played three matches, and has scored teamA = [1, 2, 3] goals in each match respectively.
7+
Football team B, has played two matches, and has scored teamB = [2, 4] goals in each match respectively.
8+
For each match of team B, compute the total number of matches of team A where team A has scored less than or equal to
9+
the number of goals scored by team B in that match.
10+
11+
###In the above case:
12+
13+
For 2 goals scored by team B in its first match, team A has 2 matches with scores 1 and 2. For 4 goals scored by team B
14+
in its second match, team A has 3 matches with scores 1, 2 and 3. Hence, the answer: [2, 3].
15+
16+
###Function Description
17+
18+
Complete the function counts in the editor below. The function must return an array of m positive integers, one for each
19+
teamB[i] representing the total number of elements teamA[j] satisfying teamA[j] = teamB[i] where 0 = j < n
20+
and 0 = i < m, in the given order.
21+
22+
###counts has the following parameter(s):
23+
24+
teamA[teamA[0],...teamA[n-1]]: first array of positive integers
25+
26+
teamB[teamB[0],...teamB[n-1]]: second array of positive integers
27+
28+
##Constraints
29+
30+
2 = n, m = 105 1 = teamA[j] = 109, where 0 = j < n. 1 = teamB[i] = 109, where 0 = i < m.
31+
32+
Input Format For Custom Testing Input from stdin will be processed as follows and passed to the function.
33+
34+
The first line contains an integer n, the number of elements in teamA.
35+
36+
The next n lines each contain an integer describing teamA[j] where 0 = j < n.
37+
38+
The next line contains an integer m, the number of elements in teamB.
39+
40+
The next m lines each contain an integer describing teamB[i] where 0 = i < m.
41+
42+
##Sample Case 0
43+
###Sample Input 0
44+
45+
4 1 4 2 4 2 3 5
46+
47+
###Sample Output 0
48+
2 4
49+
50+
###Explanation 0
51+
52+
Given values are n = 4, teamA = [1, 4, 2, 4], m = 2, and teamB = [3, 5].
53+
54+
For teamB[0] = 3, we have 2 elements in teamA (teamA[0] = 1 and teamA[2] = 2) that are = teamB[0]. For teamB[1] = 5,
55+
we have 4 elements in teamA (teamA[0] = 1, teamA[1] = 4, teamA[2] = 2, and teamA[3] = 4) that are = teamB[1].
56+
Thus, the function returns the array [2, 4] as the answer.
57+
58+
##Sample Case 1
59+
###Sample Input 1
60+
61+
5 2 10 5 4 8 4 3 1 7 8
62+
63+
###Sample Output 1
64+
65+
1 0 3 4
66+
67+
###Explanation 1
68+
69+
Given values are n = 5, teamA = [2, 10, 5, 4, 8], m = 4, and teamB = [3, 1, 7, 8].
70+
71+
For teamB[0] = 3, we have 1 element in teamA (teamA[0] = 2) that is = teamB[0]. For teamB[1] = 1, there are 0 elements
72+
in teamA that are = teamB[1]. For teamB[2] = 7, we have 3 elements in teamA (teamA[0] = 2, teamA[2] = 5,
73+
and teamA[3] = 4) that are = teamB[2]. For teamB[3] = 8, we have 4 elements in teamA (teamA[0] = 2, teamA[2] = 5,
74+
teamA[3] = 4, and teamA[4] = 8) that are = teamB[3]. Thus, the function returns the array [1, 0, 3, 4] as the answer.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.antesh.interview.nvidia;
2+
3+
/* Nvidia - Asked hacker rank coding round interview with below problems
4+
Football Scores
5+
The number of goals achieved by two football teams in matches in a league is given in the form of two
6+
lists. Consider:
7+
8+
Football team A, has played three matches, and has scored teamA = [1, 2, 3] goals in each match respectively.
9+
Football team B, has played two matches, and has scored teamB = [2, 4] goals in each match respectively.
10+
For each match of team B, compute the total number of matches of team A where team A has scored less than or equal to
11+
the number of goals scored by team B in that match.
12+
13+
In the above case:
14+
15+
For 2 goals scored by team B in its first match, team A has 2 matches with scores 1 and 2. For 4 goals scored by team B
16+
in its second match, team A has 3 matches with scores 1, 2 and 3. Hence, the answer: [2, 3].
17+
18+
Function Description
19+
20+
Complete the function counts in the editor below. The function must return an array of m positive integers, one for each
21+
teamB[i] representing the total number of elements teamA[j] satisfying teamA[j] = teamB[i] where 0 = j < n
22+
and 0 = i < m, in the given order.
23+
24+
counts has the following parameter(s):
25+
26+
teamA[teamA[0],...teamA[n-1]]: first array of positive integers
27+
28+
teamB[teamB[0],...teamB[n-1]]: second array of positive integers
29+
Constraints
30+
31+
2 = n, m = 105 1 = teamA[j] = 109, where 0 = j < n. 1 = teamB[i] = 109, where 0 = i < m.
32+
33+
Input Format For Custom Testing Input from stdin will be processed as follows and passed to the function.
34+
35+
The first line contains an integer n, the number of elements in teamA.
36+
37+
The next n lines each contain an integer describing teamA[j] where 0 = j < n.
38+
39+
The next line contains an integer m, the number of elements in teamB.
40+
41+
The next m lines each contain an integer describing teamB[i] where 0 = i < m.
42+
43+
Sample Case 0 Sample Input 0
44+
45+
4 1 4 2 4 2 3 5 Sample Output 0
46+
47+
2 4 Explanation 0
48+
49+
Given values are n = 4, teamA = [1, 4, 2, 4], m = 2, and teamB = [3, 5].
50+
51+
For teamB[0] = 3, we have 2 elements in teamA (teamA[0] = 1 and teamA[2] = 2) that are = teamB[0]. For teamB[1] = 5,
52+
we have 4 elements in teamA (teamA[0] = 1, teamA[1] = 4, teamA[2] = 2, and teamA[3] = 4) that are = teamB[1].
53+
Thus, the function returns the array [2, 4] as the answer.
54+
55+
Sample Case 1 Sample Input 1
56+
57+
5 2 10 5 4 8 4 3 1 7 8 Sample Output 1
58+
59+
1 0 3 4 Explanation 1
60+
61+
Given values are n = 5, teamA = [2, 10, 5, 4, 8], m = 4, and teamB = [3, 1, 7, 8].
62+
63+
For teamB[0] = 3, we have 1 element in teamA (teamA[0] = 2) that is = teamB[0]. For teamB[1] = 1, there are 0 elements
64+
in teamA that are = teamB[1]. For teamB[2] = 7, we have 3 elements in teamA (teamA[0] = 2, teamA[2] = 5,
65+
and teamA[3] = 4) that are = teamB[2]. For teamB[3] = 8, we have 4 elements in teamA (teamA[0] = 2, teamA[2] = 5,
66+
teamA[3] = 4, and teamA[4] = 8) that are = teamB[3]. Thus, the function returns the array [1, 0, 3, 4] as the answer.
67+
68+
*/
69+
70+
import java.util.Collection;
71+
import java.util.Collections;
72+
import java.util.List;
73+
74+
public class TwoTeamFootballMatchScoreProblem {
75+
public static void main(String[] args) {
76+
77+
}
78+
79+
public static int getCounts(List<Integer> teamA, List<Integer> teamB) {
80+
81+
Collections.sort(teamA);
82+
83+
for (int i = 0; i < teamB.size(); i++) {
84+
//brute force approach with O(n^2) time complexity
85+
for (int j = i; j < teamA.size(); j++) {
86+
87+
}
88+
89+
//binary search for each match score by team B in team A
90+
91+
}
92+
93+
return 0;
94+
}
95+
96+
//binary search for each match score by team B in team A
97+
public static int getCountsUsingBinarySearch() {
98+
99+
return 0;
100+
}
101+
}

0 commit comments

Comments
 (0)