Find all the numbers in the range which has prime set bits.

Objective: Given a range, find all the numbers in the range which has prime set bits. Example: L = 4, R = 10 Output: 5 Explanation: 4 = 1 0 0 ( not prime) 5 = 1 0 1 (prime) 6 = 1 1 0 (prime) 7 = 1 1 1 (prime) 8 = 1 … Read more Find all the numbers in the range which has prime set bits.

Check if Arithmetic Expression contains duplicate parenthesis

Objective: Given an arithmetic expression, write an algorithm to find out whether the expression contains duplicate parenthesis. Duplicate Parenthesis: Two contiguous parentheses with no elements between them can be called as duplicate parenthesis.  Example: Input: A/(B+C)*D Output: No duplicate parenthesis found. Input: A/(B+C)*D/((E+F)) Output: duplicate parenthesis found Approach: Use Stack Iterate the given expression from … Read more Check if Arithmetic Expression contains duplicate parenthesis

Determine the order of Tests when tests have dependencies on each other

Objective: You are working on a project where QA team has automated set of test cases for the project, but these test cases have to be executed in a specific order since test cases has dependencies on each other, for example if test case A and dependency on test case B then test case B … Read more Determine the order of Tests when tests have dependencies on each other

Find the maximum number present in a String

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

Convert Prefix to Postfix Expression

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

Convert Roman Number to Integer

Objective: Given a Roman number, write a program to convert it to Integer. Roman Number –   Letters used in Roman numerals and the corresponding numerical values are given in the table below. Rules:  Roman numerals are usually written in highest to lowest from left to right except for some special cases where left character … Read more Convert Roman Number to Integer

Convert Postfix to Prefix 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

String to Integer (AtoI – ASCII to Integer) – Recursive Solution

Objective: Write a recursive function to implement atoi, which converts string to an integer. Rules for converting string to an integer. If the string is null or empty, Integer will be 0. The first character of the string may represent the sign of the integer. ‘+’ or ‘-‘ Discard the white spaces if necessary. If … Read more String to Integer (AtoI – ASCII to Integer) – Recursive Solution

Grouping of Anagrams

Objective: Given an array of strings, write an algorithm to group the anagrams. Example: Input: [rat, art, cat, act, dog, god, tar, pat] Output: [rat, art, tar] [cat, act] [pat] [dog, god] Approach:  If two strings are an anagram, then characters count in both the string must match. So if we sort both the strings, … Read more Grouping of Anagrams

Print Processes and its messages from a given log file

Problem Statement:  There is an application that has several processes running in parallel and each process prints the logs in a single log file. Log message contains the process name, timestamp, message type (WARN, ERROR, etc) and actual message. You have given start timestamp and end timestamp. Write an algorithm to process the log file … Read more Print Processes and its messages from a given log file

Longest contiguous character in a given String – O(N) Solution

Objective: Given an input string, write an algorithm to find the longest contiguous character or in other words, find a maximum consecutive character in the string. Example: Input: “aaabbccccddbbaaa” Output: c, count = 4 Input: “bbbbb” Output: b, count=5 Approach: Naive Approach:  Naive Approach: Use  two nested loops The outer loop to fix it on … Read more Longest contiguous character in a given String – O(N) Solution

Convert Postfix to Infix Expression

Objective: Given a Postfix expression, write an algorithm to convert it into Infix expression. Example: Input: Postfix expression:  A B +  Output: Infix expression- (A + B) Input: Postfix expression:  ABC/-AK/L-* Output: Infix expression: ((A-(B/C))*((A/K)-L)) Approach: Use Stack Algorithm: Iterate the given expression from left to right, one character at a time If a character … Read more Convert Postfix to Infix Expression

String to Integer (AtoI – ASCII to Integer)

Objective: Implement atoi, which converts string to an integer. Rules for converting string to an integer. If the string is null or empty, Integer will be 0. The first character of string may represent the sign of the integer. ‘+’ or ‘-‘ Discard the white spaces if necessary. If any point, conversion of the string … Read more String to Integer (AtoI – ASCII to Integer)

Longest substring with at most two unique characters

Objective: Given a string, write an algorithm to find the longest substring with at most two characters. Example: Input: aabbccddc Output: ccddc, length: 5 Input: aabacbeeeebeaabb Output: beeeebe, length 7 Input: aaaaaa Output: Only one unique character is present in the input string Approach: Brute Force Identify all the substrings of the given input string … Read more Longest substring with at most two unique characters

Monotone Increasing Digits

Objective: Given a number N, find the largest number which is less than equal to N and has monotone increasing digits. Monotone Increasing Digits – If all the pairs (x, y) of adjacent digits satisfy the property, x<=y. In other words if all digits are in ascending order (can have duplicates) then digits are called … Read more Monotone Increasing Digits