In school a student gets rewarded if he has an attendance record without being absent for more than once or being late for 3 times continuously. Given a student’s attendance record represented by a string. The record only contains the following three characters: ‘A’ : Absent. ‘L’ : Late. ‘O’ : On-Time. Check whether the … Read more Student Attendance Reward
Given a string, write a function that returns a random character from the string. Example: Given string = algorithms @ tutorial horizon Random character: s Random character: r Random character: h Random character: @ Random character: i Approach: Get the length of the string and then use the java random function to get the random … Read more Get a random character from the given string – Java Program
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.
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 string A consisting of n characters and a string B consisting of m characters, write a function that will return the number of times A must be stated such that B is a substring of the repeated A. If B can never be a substring, return -1. Example: A = ‘abcd’ B =’cdabcdab’ … Read more Minimum number of times String A is repeated to such that B is substring of A
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
Given a dictionary (list of words) and an input string K. Find the largest word in the dictionary which can be obtained by deleting a few characters in the input string K. Example: Dictionary: [tutorial, horizon, trial, zon] Input: taucdtorgibalbhsoariazaonzaqn Output: tutorial Dictionary: [tutorial, horizon, trial, zon], Input: attroialled Output: trial Approach: Please read – … Read more Largest word in dictionary by removing a few characters from the given string
Objective: Given two strings, write a program to check if any of the given string is a subsequence of another string. Example if given strings are A and B then EITHER A is subsequence of B OR B is subsequence of A, program should return true. Example: A = “horizon” B = “abhordizaonr” Output: true … Read more Check if one string is a subsequence of another string.
Objective: Given two strings, source string and target string, which are permutation or anagram of each other. You are allowed two swap only consecutive characters. Write an algorithm to print all the steps ( all the swaps) which will lead the conversion of the source string to the target string. NOTE: There could be multiple … Read more Print all steps to convert one string to another string
Objective: Given an array of integers (in particular order or permutation of a set of numbers), write an algorithm to find the lexicographically previous permutation of the given permutation with only one swap. This problem can also be asked as “Given a permutation of numbers you need to find the next smaller premutation OR largest … Read more Lexicographically previous permutation With One swap
Objective: Given two strings with equal lengths, write an algorithm to calculate the hamming distance between the strings. Hamming Distance: Hamming distance between two strings is the number of positions at which the characters are different. Example: X = AABBCCDD, Y = AAAACCCC Hamming distance: 4 There are four positions at which bits are different … Read more Hamming Distance between two given strings
Given a string with alphabets and numbers. Write an algorithm to find the maximum number in the string. If any number is greater than equal to Max_Value of integer then return the Max_Value. Example: Input: “abcd 45, 54 and 50 are the numbers” Output: Maximum Number- 54 Input: aabb123ccdefgh54319hij Output: 54319 Input: 213123123and544455523412423423424234234234 Output: 2147483647 … Read more Find the maximum number present in a String
Objective: Given a Prefix expression, write an algorithm to convert it into Postfix expression. Example: Input: Prefix expression: + A B Output: Postfix expression: A B + Input: Prefix expression: *-A/BC-/AKL Output: Postfix expression: ABC/-AK/L-* Approach: Use Stacks Algorithm: Iterate the given expression from right to left, one character at a time If the character … Read more Convert Prefix to Postfix Expression
Objective: Given a Postfix expression, write an algorithm to convert it into prefix expression. Example: Input: Postfix expression: A B + Output: Prefix expression- + A B Input: Postfix expression: ABC/-AK/L-* Output: Infix expression: *-A/BC-/AKL Approach: Use Stack Algorithm: Iterate the given expression from left to right, one character at a time If the character … Read more Convert Postfix to Prefix Expression
Objective: Given an Integer, write a program to convert it to Roman number. This problem can be stated as “Convert Decimal to Roman String” OR “Convert numeral value to Roman numeral”. Roman Number – Letters used in Roman numerals and the corresponding numerical values are given in the table below. Rules: Roman numerals are … Read more Convert Integer to Roman