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

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.

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

Calculate (x^y)%z without using pow() function

Problem: Given integers x, y, and z. Write a program to calculate (x^y)%z without pow() function. Example: x = 2, y= 5, z = 3 Output: (2 ^ 5) % 3 = 2 x = 5, y= 55, z = 221 Output: (5 ^ 55) % 221 = 112 Approach: Straight forward way to calculate … Read more Calculate (x^y)%z without using pow() function

Efficient Robot Problem – Find Minimum Trips

Problem: There is N number of items that need to be transferred from one place to another by a robot. Each item has a specific weight. The robot can carry maximum weight K in one trip. You need to come up with an algorithm to find out the minimum number of trips that the robot … Read more Efficient Robot Problem – Find Minimum Trips

Job Sequencing algorithm – Java

Objective: You are given n jobs along with the deadline and profit for each job. Your task is to write an algorithm to choose the jobs wisely which can maximize the profit. Also compute the maximum profit. Below are the details Each job duration is 1 unit. Name – Name of the job. Deadline – … Read more Job Sequencing algorithm – Java

Find subarray with a sum to given number-2 | Handle negative numbers

Problem: Given an array (positive and negative) and an integer, find the subarray with sum is equal to the given integer.  Note: This problem is an extension of – find the subarray with sum to a Given Value, in which arrays with negative numbers are not handled.  Example: Given input: [25, 12, -14, 22, -19, … Read more Find subarray with a sum to given number-2 | Handle negative numbers