Candidate Voting Problem

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

Number of Contiguous Parking Areas

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

Design data structure for players and ranks

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

Minimum Deletions to make the occurrence of each character unique.

Objective: Given a string, your task is to delete the minimum number of characters in the string so that the count of each character is unique. Example: Input = “aaaaabbbbb” Output: 1 Explanation: In the input string, characters ‘a’ and ‘b’, both occur 5 times. You can delete either one ‘a’ or one ‘b’ to … Read more Minimum Deletions to make the occurrence of each character unique.

Valid Pickup and Delivery options

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

Compare two version numbers of a software

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

The largest number can be formed from the given number

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

Print all nested directories and files in a given directory – Recursion

Given a source directory, write a recursive program to print all the nested directories and files in the source directory. Example: [Hellojava] [target] original-Hellojava-1.0-SNAPSHOT.jar [classes] [com] [example] [lambda] [demo] Hello.class [maven-archiver] pom.properties Hellojava-1.0-SNAPSHOT.jar pom.xml Hellojava.iml [src] [test] [java] [main] [resources] [java] [com] Hello.java Approach: Check if given file is directory,  if yes then print its … Read more Print all nested directories and files in a given directory – Recursion

Maximum meetings in one room

You have one meeting room at your company. There are N meeting needs to take place. Every meeting has a start time and end time along with the meeting title. Your task is to schedule as many meetings as possible in that conference room with no conflicts.  Example: Meetings: A[ Start Time: 1, End Time: … Read more Maximum meetings in one room

The number of cycles in a given array of integers.

Objective: Given an array of size N which contains integers from range 0 to N-1. (No duplicates). Write a program to find the number of cycles in the array.  Cycles in Array: Since the array is of size N and elements are from 0 to N-1 without any duplicates means all the elements appear exactly … Read more The number of cycles in a given array of integers.

Check if given sudoku is valid or not

Problem: Given a filled sudoku, write a program to check if sudoku is valid or following all of its rules. SUDOKU rules:  Each column contains all of the digits from 1 to 9 only once. Each row contains all of the digits from 1 to 9 only once. Each of the nine 3×3 sub-grid contains … Read more Check if given sudoku is valid or not

Construct the Largest number from the given digits

Given a set of digits, write an algorithm to construct the largest number possible by appending the given digits. Example: Given Digits: [9, 1, 9, 2, 8, 4, 2] largest number: 9984221 Given Digits: [1, 2, 5, 6, 7] largest number: 76521 Approach: Sorting Sort the given array in descending order. Initialize result = 0. … Read more Construct the Largest number from the given digits

Find Lexicographically smallest or largest substring of size k

Given a string and number k, write a program to find the lexicographically smallest substring of size k. Note: This problem can also be asked as find the lexicographically largest substring of size k Example: Input: “tutorial”, k = 2 Output: Smallest Lexicographically SubString: “al” Input: “tutorial”, k=3 Smallest Lexicographically SubString: “ial” Input: horizon, k=2 … Read more Find Lexicographically smallest or largest substring of size k