Skip to content

Commit edf5311

Browse files
author
Anteshkumar Sharma
authored
Merge pull request #5 from MissionJava/Alten_codility_test
Alten codility test
2 parents a76b163 + bc52db6 commit edf5311

File tree

7 files changed

+413
-9
lines changed

7 files changed

+413
-9
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# data-structure-and-algorithm-using-java
2+
3+
This repository includes data structure and algorithm solutions, and solutions of the live coding interview questions asked at different companies over a video call.

src/main/java/com/antesh/agoda/App.java

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.antesh.interview.agoda;
2+
3+
/*
4+
* Suppose we
5+
have a rod of length 5, which can be cut into smaller pieces of integer lengths 1, 2, 3, or 4. These smaller rods
6+
can then be further cut into smaller pieces. We stress that the rod pieces can only be cut into pieces of integer
7+
length; so pieces of size 1/2 or π are not considered in this problem. Now suppose that we can sell rods of
8+
length 1 for $1, which we denote p1 = 1. Similarly, suppose that the prices for rods of length 2, . . . , 5 are given
9+
by p2 = 4, p3 = 7, p4 = 8, and p5 = 9 respectively. We make two key assumptions: we will sell all the smaller
10+
rods, regardless of the cuts; and that each cut is free. Under these assumptions, how should the rod be cut to
11+
maximize the profit? We note the following cuts and the corresponding profits.
12+
- If the rod is cut into five pieces of length 1, we stand to make 5 · p1 = $5.
13+
- If the rod is cut into one piece of length 2 and one piece of length 3, we stand to make p2+p3 = 4+7 = $11.
14+
-If the rod is cut into two pieces of length 2 and one piece of length 1, we stand to make 2 · p2 + p1 =
15+
8 + 1 = $9.
16+
Out of the above options, cutting the rod into one piece of length 2 and one rod of length 3 is the most
17+
profitable. Of course, there are other possible cuts not listed above, such as cutting the rod into one piece
18+
of length 1 and one piece of length 4. The goal is to determine the most profitable cut. The Rod-Cutting
19+
Problem is formalized as follows.
20+
Definition 1 (Rod-Cutting Problem).
21+
- Instance: Let n ∈ N be the length of the rod, and let p1, p2, . . . , pn be non-negative real numbers. Here,
22+
pi
23+
is the price of a length i rod.
24+
- Solution: The maximum revenue, which we denote rn, obtained by cutting the rod into smaller pieces
25+
of integer lengths and selling the smaller rods
26+
27+
* Example:
28+
* For input N = 4, v[i] = {0, 2, 4, 7, 7}
29+
* Output: 2 (cuts of length 1 and 3 gives revenue of 2 + 7 = 9)
30+
* Explanation:
31+
In below price by rod length array,
32+
price 0 at index i=0 is for if rod length: 0,
33+
price 2 at index i = 1 is for rod length 1
34+
price 4 at index i = 2 is for rod length 2
35+
price 7 at index i = 3 is for rod length 3
36+
price 7 at index i = 4 is for rod length 4
37+
so on can be extended...
38+
39+
Input:
40+
int[] prices = { 2, 4, 7, 7};
41+
int rod_size = 4;
42+
Output:
43+
9
44+
* */
45+
public class MaximumRevenueFromRodCuttingProblem {
46+
47+
public static void main(String[] args) {
48+
System.out.println(solution(4, new int[]{2, 4, 7, 7}));
49+
System.out.println(solution(8, new int[]{1, 5, 8, 9, 10, 17, 17, 20}));
50+
System.out.println(solution(8, new int[]{3, 5, 8, 9, 10, 17, 17, 20}));
51+
52+
}
53+
54+
private static int solution(int rod_size, int[] prices) {
55+
56+
int max_profit = Integer.MIN_VALUE;
57+
int profit = 0;
58+
59+
if (rod_size == 1) {
60+
return prices[0];
61+
}
62+
63+
for (int i = rod_size - 1; i >= 0; i--) {
64+
profit = profit + prices[i];
65+
int remain = rod_size - i - 1;
66+
67+
if (remain > 0) {
68+
profit = profit + solution(remain, prices);
69+
}
70+
max_profit = Math.max(max_profit, profit);
71+
profit = 0;
72+
}
73+
74+
return max_profit;
75+
}
76+
77+
}

src/main/java/com/antesh/alten/codility/SolutionIter.java renamed to src/main/java/com/antesh/interview/alten/SolutionIter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.antesh.alten.codility;
1+
package com.antesh.interview.alten;
22

33
import java.io.BufferedReader;
44
import java.io.FileReader;
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.

0 commit comments

Comments
 (0)