From 3b445a3150a47215fb28843aa5e22fa27658ceae Mon Sep 17 00:00:00 2001 From: Antesh Sharma Date: Sun, 13 Feb 2022 01:55:04 +0530 Subject: [PATCH 1/4] HackerRank coding at Agoda -Maximum profit by selling rod pieces of size N into 1..N size --- .../MaximumRevenueFromRodCuttingProblem.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/main/java/com/antesh/agoda/MaximumRevenueFromRodCuttingProblem.java diff --git a/src/main/java/com/antesh/agoda/MaximumRevenueFromRodCuttingProblem.java b/src/main/java/com/antesh/agoda/MaximumRevenueFromRodCuttingProblem.java new file mode 100644 index 0000000..e95ec6c --- /dev/null +++ b/src/main/java/com/antesh/agoda/MaximumRevenueFromRodCuttingProblem.java @@ -0,0 +1,77 @@ +package com.antesh.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; + } + +} From 3d63e40b3d1aa5535970027cafb5706fe8b866fd Mon Sep 17 00:00:00 2001 From: Antesh Sharma Date: Sun, 13 Feb 2022 02:03:38 +0530 Subject: [PATCH 2/4] Code refactor --- src/main/java/com/antesh/agoda/App.java | 8 -------- .../agoda/MaximumRevenueFromRodCuttingProblem.java | 2 +- .../{alten/codility => interview/alten}/SolutionIter.java | 2 +- 3 files changed, 2 insertions(+), 10 deletions(-) delete mode 100644 src/main/java/com/antesh/agoda/App.java rename src/main/java/com/antesh/{ => interview}/agoda/MaximumRevenueFromRodCuttingProblem.java (98%) rename src/main/java/com/antesh/{alten/codility => interview/alten}/SolutionIter.java (98%) diff --git a/src/main/java/com/antesh/agoda/App.java b/src/main/java/com/antesh/agoda/App.java deleted file mode 100644 index 2d078fe..0000000 --- a/src/main/java/com/antesh/agoda/App.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.antesh.agoda; - -public class App { - - public static void main(String[] args) { - - } -} diff --git a/src/main/java/com/antesh/agoda/MaximumRevenueFromRodCuttingProblem.java b/src/main/java/com/antesh/interview/agoda/MaximumRevenueFromRodCuttingProblem.java similarity index 98% rename from src/main/java/com/antesh/agoda/MaximumRevenueFromRodCuttingProblem.java rename to src/main/java/com/antesh/interview/agoda/MaximumRevenueFromRodCuttingProblem.java index e95ec6c..2d6ec8a 100644 --- a/src/main/java/com/antesh/agoda/MaximumRevenueFromRodCuttingProblem.java +++ b/src/main/java/com/antesh/interview/agoda/MaximumRevenueFromRodCuttingProblem.java @@ -1,4 +1,4 @@ -package com.antesh.agoda; +package com.antesh.interview.agoda; /* * Suppose we diff --git a/src/main/java/com/antesh/alten/codility/SolutionIter.java b/src/main/java/com/antesh/interview/alten/SolutionIter.java similarity index 98% rename from src/main/java/com/antesh/alten/codility/SolutionIter.java rename to src/main/java/com/antesh/interview/alten/SolutionIter.java index 050a0c9..2d6cd8f 100644 --- a/src/main/java/com/antesh/alten/codility/SolutionIter.java +++ b/src/main/java/com/antesh/interview/alten/SolutionIter.java @@ -1,4 +1,4 @@ -package com.antesh.alten.codility; +package com.antesh.interview.alten; import java.io.BufferedReader; import java.io.FileReader; From 54b1efe2c5885c107802eaab7f6369756fe7b4d1 Mon Sep 17 00:00:00 2001 From: Anteshkumar Sharma Date: Sun, 13 Feb 2022 02:11:17 +0530 Subject: [PATCH 3/4] Updated readme file --- README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..92cf239 --- /dev/null +++ b/README.md @@ -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. From bc52db6196641f0f096710b1702ceec730b259dc Mon Sep 17 00:00:00 2001 From: Antesh Sharma Date: Wed, 16 Feb 2022 20:25:39 +0530 Subject: [PATCH 4/4] Added solutions for card deck and two team football match score problem --- .../interview/nvidia/CardDeckProblem.java | 157 ++++++++++++++++++ .../com/antesh/interview/nvidia/ReadMe.md | 74 +++++++++ .../TwoTeamFootballMatchScoreProblem.java | 101 +++++++++++ 3 files changed, 332 insertions(+) create mode 100644 src/main/java/com/antesh/interview/nvidia/CardDeckProblem.java create mode 100644 src/main/java/com/antesh/interview/nvidia/ReadMe.md create mode 100644 src/main/java/com/antesh/interview/nvidia/TwoTeamFootballMatchScoreProblem.java diff --git a/src/main/java/com/antesh/interview/nvidia/CardDeckProblem.java b/src/main/java/com/antesh/interview/nvidia/CardDeckProblem.java new file mode 100644 index 0000000..f83591a --- /dev/null +++ b/src/main/java/com/antesh/interview/nvidia/CardDeckProblem.java @@ -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 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 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 uniqueCardDeck = getUniqueCardDeck(cardsDeck); + findMissingCards(prePopulatedCardDeck, uniqueCardDeck); + } + + public static void testNoMissingCards(Set 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 uniqueCardDeck = getUniqueCardDeck(cardsDeck); + findMissingCards(prePopulatedCardDeck, uniqueCardDeck); + } + + public static void testMissingCards(Set 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 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 getUniqueCardDeck(String[] cardsDeck) { + Set 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 prePopulatedCardDeck, Set uniqueCardDeck) { + Set 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 prepopulateUniqueCards() { + + Set 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; + + } +} diff --git a/src/main/java/com/antesh/interview/nvidia/ReadMe.md b/src/main/java/com/antesh/interview/nvidia/ReadMe.md new file mode 100644 index 0000000..572b9df --- /dev/null +++ b/src/main/java/com/antesh/interview/nvidia/ReadMe.md @@ -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. diff --git a/src/main/java/com/antesh/interview/nvidia/TwoTeamFootballMatchScoreProblem.java b/src/main/java/com/antesh/interview/nvidia/TwoTeamFootballMatchScoreProblem.java new file mode 100644 index 0000000..9d28c2e --- /dev/null +++ b/src/main/java/com/antesh/interview/nvidia/TwoTeamFootballMatchScoreProblem.java @@ -0,0 +1,101 @@ +package com.antesh.interview.nvidia; + +/* Nvidia - Asked hacker rank coding round interview with below problems +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. + +*/ + +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +public class TwoTeamFootballMatchScoreProblem { + public static void main(String[] args) { + + } + + public static int getCounts(List teamA, List teamB) { + + Collections.sort(teamA); + + for (int i = 0; i < teamB.size(); i++) { + //brute force approach with O(n^2) time complexity + for (int j = i; j < teamA.size(); j++) { + + } + + //binary search for each match score by team B in team A + + } + + return 0; + } + + //binary search for each match score by team B in team A + public static int getCountsUsingBinarySearch() { + + return 0; + } +}