Skip to content

Alten codility test #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# data-structure-and-algorithm-using-java

This repository includes data structure and algorithm solutions, and solutions of the live coding interview questions asked at different companies over a video call.
8 changes: 0 additions & 8 deletions src/main/java/com/antesh/agoda/App.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.antesh.interview.agoda;

/*
* Suppose we
have a rod of length 5, which can be cut into smaller pieces of integer lengths 1, 2, 3, or 4. These smaller rods
can then be further cut into smaller pieces. We stress that the rod pieces can only be cut into pieces of integer
length; so pieces of size 1/2 or π are not considered in this problem. Now suppose that we can sell rods of
length 1 for $1, which we denote p1 = 1. Similarly, suppose that the prices for rods of length 2, . . . , 5 are given
by p2 = 4, p3 = 7, p4 = 8, and p5 = 9 respectively. We make two key assumptions: we will sell all the smaller
rods, regardless of the cuts; and that each cut is free. Under these assumptions, how should the rod be cut to
maximize the profit? We note the following cuts and the corresponding profits.
- If the rod is cut into five pieces of length 1, we stand to make 5 · p1 = $5.
- 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.
-If the rod is cut into two pieces of length 2 and one piece of length 1, we stand to make 2 · p2 + p1 =
8 + 1 = $9.
Out of the above options, cutting the rod into one piece of length 2 and one rod of length 3 is the most
profitable. Of course, there are other possible cuts not listed above, such as cutting the rod into one piece
of length 1 and one piece of length 4. The goal is to determine the most profitable cut. The Rod-Cutting
Problem is formalized as follows.
Definition 1 (Rod-Cutting Problem).
- Instance: Let n ∈ N be the length of the rod, and let p1, p2, . . . , pn be non-negative real numbers. Here,
pi
is the price of a length i rod.
- Solution: The maximum revenue, which we denote rn, obtained by cutting the rod into smaller pieces
of integer lengths and selling the smaller rods

* Example:
* For input N = 4, v[i] = {0, 2, 4, 7, 7}
* Output: 2 (cuts of length 1 and 3 gives revenue of 2 + 7 = 9)
* Explanation:
In below price by rod length array,
price 0 at index i=0 is for if rod length: 0,
price 2 at index i = 1 is for rod length 1
price 4 at index i = 2 is for rod length 2
price 7 at index i = 3 is for rod length 3
price 7 at index i = 4 is for rod length 4
so on can be extended...

Input:
int[] prices = { 2, 4, 7, 7};
int rod_size = 4;
Output:
9
* */
public class MaximumRevenueFromRodCuttingProblem {

public static void main(String[] args) {
System.out.println(solution(4, new int[]{2, 4, 7, 7}));
System.out.println(solution(8, new int[]{1, 5, 8, 9, 10, 17, 17, 20}));
System.out.println(solution(8, new int[]{3, 5, 8, 9, 10, 17, 17, 20}));

}

private static int solution(int rod_size, int[] prices) {

int max_profit = Integer.MIN_VALUE;
int profit = 0;

if (rod_size == 1) {
return prices[0];
}

for (int i = rod_size - 1; i >= 0; i--) {
profit = profit + prices[i];
int remain = rod_size - i - 1;

if (remain > 0) {
profit = profit + solution(remain, prices);
}
max_profit = Math.max(max_profit, profit);
profit = 0;
}

return max_profit;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.antesh.alten.codility;
package com.antesh.interview.alten;

import java.io.BufferedReader;
import java.io.FileReader;
Expand Down
157 changes: 157 additions & 0 deletions src/main/java/com/antesh/interview/nvidia/CardDeckProblem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
Coding Round 3 - at Nvidia
* // 1. check all 52 cards available or not
// 2. check which card is missing
// 3. check if we have a duplicate card


String[] cardsDeck = new String[]
{ "AH", "KH", "10D", "5H", "AC", "10S", "AS", "8H", "9D", "5S", "3D", "7S", "2D", "JH",
"QS", "2H", "QD", "6S", "5D", "8D", "2C", "JC", "KS", "KD", "4H", "3H", "3C", "6D", "QH", "9H", "JD", "7D",
"AD", "2S", "3S", "4S", "9S", "9C", "5C", "7C", "QC", "10H", "10C", "8S", "JS", "4D", "6H", "4C", "8C",
"7H", "6C", "KC" };

Cards: A J Q K, 2 - 10
Suit
D - Diamond
S - Spade
H - Heart
C - Club
*
*
*
*
* */

import java.util.*;
import java.util.stream.Collectors;

public class CardDeckProblem {
public static void main(String[] args) {

Set<String> prePopulatedCardDeck = prepopulateUniqueCards();

testNullCardDeck();
test52CardsWithEmptyCardDeck();
testDuplicateAndMissingCards(prePopulatedCardDeck);
testMissingCards(prePopulatedCardDeck);
testNoMissingCards(prePopulatedCardDeck);

}


public static void testNullCardDeck() {
validateCardDeck(null);
}

public static void test52CardsWithEmptyCardDeck() {
String[] cardsDeck = new String[]{"AH"};
validateCardDeck(cardsDeck);
}

public static void testDuplicateAndMissingCards(Set<String> prePopulatedCardDeck) {
//duplicate AH
String[] cardsDeck = new String[]
{"AH", "AH", "10D", "5H", "AC", "10S", "AS", "8H", "9D", "5S", "3D", "7S", "2D", "JH",
"QS", "2H", "QD", "6S", "5D", "8D", "2C", "JC", "KS", "KD", "4H", "3H", "3C", "6D", "QH", "9H", "JD", "7D",
"AD", "2S", "3S", "4S", "9S", "9C", "5C", "7C", "QC", "10H", "10C", "8S", "JS", "4D", "6H", "4C", "8C",
"7H", "6C", "KC"};
Set<String> uniqueCardDeck = getUniqueCardDeck(cardsDeck);
findMissingCards(prePopulatedCardDeck, uniqueCardDeck);
}

public static void testNoMissingCards(Set<String> prePopulatedCardDeck) {
//All valid 52 cards
String[] cardsDeck = new String[]
{"AH", "KH", "10D", "5H", "AC", "10S", "AS", "8H", "9D", "5S", "3D", "7S", "2D", "JH",
"QS", "2H", "QD", "6S", "5D", "8D", "2C", "JC", "KS", "KD", "4H", "3H", "3C", "6D", "QH", "9H", "JD", "7D",
"AD", "2S", "3S", "4S", "9S", "9C", "5C", "7C", "QC", "10H", "10C", "8S", "JS", "4D", "6H", "4C", "8C",
"7H", "6C", "KC"};
Set<String> uniqueCardDeck = getUniqueCardDeck(cardsDeck);
findMissingCards(prePopulatedCardDeck, uniqueCardDeck);
}

public static void testMissingCards(Set<String> prePopulatedCardDeck) {
//missing AK, AH
String[] cardsDeck = new String[]
{"ZK", "ZK", "10D", "5H", "AC", "10S", "AS", "8H", "9D", "5S", "3D", "7S", "2D", "JH",
"QS", "2H", "QD", "6S", "5D", "8D", "2C", "JC", "KS", "KD", "4H", "3H", "3C", "6D", "QH", "9H", "JD", "7D",
"AD", "2S", "3S", "4S", "9S", "9C", "5C", "7C", "QC", "10H", "10C", "8S", "JS", "4D", "6H", "4C", "8C",
"7H", "6C", "KC"};
Set<String> uniqueCardDeck = getUniqueCardDeck(cardsDeck);
findMissingCards(prePopulatedCardDeck, uniqueCardDeck);
}

private static void validateCardDeck(String[] cardsDeck) {
if (cardsDeck == null || cardsDeck.length == 0) {
System.out.println("Invalid input or empty input");
return;
}

if (cardsDeck.length < 52) {
System.out.println("Number of cards in the deck are less than 52");
}
}

private static Set<String> getUniqueCardDeck(String[] cardsDeck) {
Set<String> uniqueCardDeck = new TreeSet<>();
StringBuilder duplicate = new StringBuilder();
for (String card : cardsDeck) {
boolean result = uniqueCardDeck.add(card);
if (!result) {
duplicate.append(card + " ");
}
}
String duplicateMessage = duplicate.toString().length() > 0 ? "Found duplicate card: " + duplicate.toString() : "No duplicate card found";
System.out.println(duplicateMessage);
return uniqueCardDeck;
}

private static void findMissingCards(Set<String> prePopulatedCardDeck, Set<String> uniqueCardDeck) {
Set<String> missingCards = prePopulatedCardDeck.stream().filter(prePopulatedCard -> {
return uniqueCardDeck.stream().noneMatch(givenCard -> {
return prePopulatedCard.equals(givenCard);
});
}).collect(Collectors.toSet());

String missingCardMessage = missingCards.size() > 0 ? "Missing cards: " + missingCards : "No missing card";
System.out.println(missingCardMessage);
}


private static Set<String> prepopulateUniqueCards() {

Set<String> uniqueCardDeck = new TreeSet<>();

for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) {
StringBuilder sb = new StringBuilder();
if (j == 1) {
sb.append("A");
} else if (j == 11) {
sb.append("J");
} else if (j == 12) {
sb.append("Q");
} else if (j == 13) {
sb.append("K");
} else {
sb.append(j);
}

if (i == 0) {
sb.append("D");
} else if (i == 1) {
sb.append("S");
} else if (i == 2) {
sb.append("H");
} else if (i == 3) {
sb.append("C");
}
uniqueCardDeck.add(sb.toString());
}
}

return uniqueCardDeck;

}
}
74 changes: 74 additions & 0 deletions src/main/java/com/antesh/interview/nvidia/ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#HackerRank coding round interview asked at Nvidia
##Football Scores
The number of goals achieved by two football teams in matches in a league is given in the form of two
lists. Consider:

Football team A, has played three matches, and has scored teamA = [1, 2, 3] goals in each match respectively.
Football team B, has played two matches, and has scored teamB = [2, 4] goals in each match respectively.
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
the number of goals scored by team B in that match.

###In the above case:

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
in its second match, team A has 3 matches with scores 1, 2 and 3. Hence, the answer: [2, 3].

###Function Description

Complete the function counts in the editor below. The function must return an array of m positive integers, one for each
teamB[i] representing the total number of elements teamA[j] satisfying teamA[j] = teamB[i] where 0 = j < n
and 0 = i < m, in the given order.

###counts has the following parameter(s):

teamA[teamA[0],...teamA[n-1]]: first array of positive integers

teamB[teamB[0],...teamB[n-1]]: second array of positive integers

##Constraints

2 = n, m = 105 1 = teamA[j] = 109, where 0 = j < n. 1 = teamB[i] = 109, where 0 = i < m.

Input Format For Custom Testing Input from stdin will be processed as follows and passed to the function.

The first line contains an integer n, the number of elements in teamA.

The next n lines each contain an integer describing teamA[j] where 0 = j < n.

The next line contains an integer m, the number of elements in teamB.

The next m lines each contain an integer describing teamB[i] where 0 = i < m.

##Sample Case 0
###Sample Input 0

4 1 4 2 4 2 3 5

###Sample Output 0
2 4

###Explanation 0

Given values are n = 4, teamA = [1, 4, 2, 4], m = 2, and teamB = [3, 5].

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,
we have 4 elements in teamA (teamA[0] = 1, teamA[1] = 4, teamA[2] = 2, and teamA[3] = 4) that are = teamB[1].
Thus, the function returns the array [2, 4] as the answer.

##Sample Case 1
###Sample Input 1

5 2 10 5 4 8 4 3 1 7 8

###Sample Output 1

1 0 3 4

###Explanation 1

Given values are n = 5, teamA = [2, 10, 5, 4, 8], m = 4, and teamB = [3, 1, 7, 8].

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
in teamA that are = teamB[1]. For teamB[2] = 7, we have 3 elements in teamA (teamA[0] = 2, teamA[2] = 5,
and teamA[3] = 4) that are = teamB[2]. For teamB[3] = 8, we have 4 elements in teamA (teamA[0] = 2, teamA[2] = 5,
teamA[3] = 4, and teamA[4] = 8) that are = teamB[3]. Thus, the function returns the array [1, 0, 3, 4] as the answer.
Loading