Many times we have a list of custom objects and we need to sort these objects. In such cases we need to override the compare function for Collections.sort method. In this article we discuss how to do that. Say we have a list of employees. Each employee has id, name, and salary. Write a function … Read more Override List Collections Sort Method
Given two arrays of integers. Write a program to find the sum of all the overlapping elements between two arrays. You can consider that there will not be any duplicates in each array individually. Example: A  : [6, 5, 1, 9, 2, 8, 3] B  : [3, 7, 9, 2, 4] Overlapping sum … Read more Overlapping sum of two arrays.
Given candidates standing for an election, design a data structure that can support the following modules – 1. voteCandidate (candidateName) – Add one vote for the candidate. 2. getTopK ( k ) – This will return top K candidates at that time. It can return more than k candidates if more candidates have the same … Read more Candidate Voting Problem
Problem: Given a building with parking slots. If a spot is free then it is marked by 1 and if the spot is taken by a vehicle then it is marked by 0. Write a program to find the number of free contiguous parking areas in the building. One free parking area can have one … Read more Number of Contiguous Parking Areas
Given a row index K, write a program to print the Kth of Pascal triangle. Note: row index starts from 0. Pascal Triangle: Example: K = 2 Output: 1, 1 K= 5 Output: 1, 4, 6, 4, 1 Simple Approach: As we discussed here – Pascal triangle, starting calculating the rows from 1 to K … Read more Pascal Triangle – Get specific row K
Given a list of player names and their scores, design a data structure that can support following modules in optimal time- updateEntry(String name) – Increase the player score with “name” by 1. If no player with name is present then add a player with score 1. getEntryFromRank(int rank) – Get the players with the given … Read more Design data structure for players and ranks
Problem: Given a bipartite graph, write an algorithm to find the maximum matching. The maximum bipartite matching solves many problems in the real world like if there are M jobs and N applicants. Each applicant can do some jobs. Your task is to assign these jobs to the applicants so that maximum applicants get the … Read more Maximum Bipartite Matching Problem
Many times we have an array of custom objects and we need to sort these objects. In such cases we need to override the compare function for Arrays.sort method. In this article we discuss how to do that. Say we have a list of employees. Each employee has id, name, and salary. Write a function … Read more Override Arrays Sort Method – Java
Given a number N, write a program to print the first N lines of the Pascal triangle. Pascal Triangle: Note: In Pascal’s triangle, each number is the sum of the two numbers directly above it. (Image reference: Wiki) Approach: Initialize list row and list of lists as all_rows. We will keep updating the list row … Read more Pascal Triangle
Given a list of tax brackets (different tax on different incomes). Write a program to calculate the tax on given income. Tax bracket is a pair consisting of amount and percentage tax on that amount. If amount is null means, it’s a highest bracket and percentage tax is mentioned will be applied to all the … Read more Calculate tax on income as per given tax brackets.
Given N orders, each order consists of pickup and delivery services, means delivery of a particular service will after the pick up of the same service so the sequence pickup/delivery such that delivery(i) is after pickup(i). Write a program to count all valid pickup/delivery possible sequences Since the answer may be too large, return it … Read more Valid Pickup and Delivery options
Given a list L of video names and their watch rates, write a function that will return the videos with the top 10 watch rates. Video names may appear more than once. Example: Input: [abc : 10] [def : 20] [abc : 15] [ghi : 50] [xyz : 100] [abc : 25] [jkl : 10] … Read more Print Top 10 videos from List
Given two versions of a software, write an algorithm to find out which version is the latest. Example: Version A: 1.1.0 Version B: 1.1.1 Given version B latest than version A Version A: 1.2.1 Version B: 1.2.1 Given versions are the same Version A: 2.2.0 Version B: 2.2 Given versions are the same Version A: … Read more Compare two version numbers of a software
Given a number write an algorithm to construct the largest number possible by using the digits of given number. Given number could be a negative number as well. Example: Given Input: 34277765 Output: 77765432 Given Input: -342765 Output: -234567 Given Input: 0 Output: 0 Given Input: 2034 Output: 4320 Approach: Sorting Check if the given … Read more The largest number can be formed from the given number
Given an array of integers, you are allowed to swap only adjacent elements in the array. write a program to find the minimum number of swaps to sort the given array. Example: Input : [2, 20, 15, 6, 10] Minimums adjacent swaps required sort the array: 5 Input : [10, 3, 4, 2, 5, 7, … Read more Minimum number of adjacent swaps to sort the given array