HOW TO CRACK TOUGH
TECHNICAL QUESTIONS
Version
1
Table of Contents
Strings…………………………………………………………….....1
S T R I N G S
1111
Question 1: Write a program to find if a string has all unique characters. To solve this problem efficiently, we need a structure to record the occurrence of a character as we traverse the string. If we are allowed to use additional space, this problem can be solved in O(n) time complexity where n is the number of characters in the string. A bitmap will be the most efficient data structure to record this information, given the factor that there are only a finite number of characters which can occur. For interview purposes, you can always reduce the complexity by assuming ASCII character set which reduces the number of characters which can occur to 256. Since C++ doesn’t have a bit type, this can be achieved with Integer data type. Assuming Integer is 4 bytes (each byte is 8 bits), an integer will have 32 bits. 8 integers (256/32) are needed to represent the state of 256 characters. Sample Program : #include <iostream> #include <stdio.h> #include <string> using namespace std; //Implement a function to see if all characters in the string are unique bool isUnique(string strToCheck) { int charOccurrence[8]; // Assuming 256 character set //Intialize int array for ( int i = 0 ; i < 8 ; i++) charOccurrence[i] =0 ; for (size_t index = 0; index < strToCheck.length(); in dex++) { //Pick the right integer and then compare the bit a t the position determined by the ASCII code of the charac ter. //Set the bit to 1 if the character is found. If th e bit is already 1 return false. if ( (charOccurrence[ strToCheck[index] / ( 8 * 4 ) ] & (1 >> ( strToCheck[index] % (8 * 4) ))) == 0 ) charOccurrence[ strToCheck[index] / ( 8 * 4 ) ] = charOccurrence[ strToCheck[index] / ( 8 * 4 ) ] | ( 1 >> ( strToCheck[index] % (8 * 4))); else
Chapter
1
S T R I N G S
2222
return false ; } return true ; }
Without using additional memory, the time complexity that can be achieved is O(n2). Pick each character at a time and compare it with the remaining characters in the string to verify the uniqueness.
S T R I N G S
3333
Question 2: Given a number as a string, represent it as a decimal number. Now can you generalize it, for a number with any base representation of the number instead of just 10?
#include <iostream.h> #include <stdio.h> using namespace std; int covertStringToNumber(string number) { int result = 0; size_t index =0; bool isNegative= false ; //Take into account a negative number if ( number[0] == '-' ) isNegative = true ; //Increment index by 1 if the +/- occur before the number if (number[0] == '-' || number[0] == '+' ) ++index; for (;index < number.length(); index++) { //Verify that the character is an integer if ( number[index] >= ( int ) '0' && number[index] <= ( int ) '9' ) { result = result * 10 + (number[index] - ( int ) '0' ); } else throw "Invalid number passed - " + number; } if (isNegative) result*= -1; return result; }
Generalized: #include <iostream.h> #include <stdio.h> using namespace std; int covertStringToNumber(string number, int base) { int result = 0; short int digit = 0; size_t index =0; bool isNegative= false ; //Take into account a negative number if ( number[0] == '-' ) isNegative = true ;
S T R I N G S
4444
//Increment index by 1 if the +/- occur before the number if (number[0] == '-' || number[0] == '+' ) ++index; for (;index < number.length(); index++) { //Verify that the character is an integer if ( ( base <=10 && (number[index] >= ( int ) '0' && number[index] <= (( int ) '0' + (base-1))) ) || ( base > 10 && (number[index] >= ( int ) '0' && number[index] <= ( int ) '9' || (number[index] >= 'A' && number[index] <= ( 'A' +( base-11))) ) )){ if ( base > 10 && (number[index] >= 'A' && number[index] <= ( 'A' + (base-11))) ){ digit = 10 + (number[index] - 'A' ); } else { digit = number[index]- '0' ; } result = result * base + digit; } else throw "Invalid number passed - " + number; } if (isNegative) result*= -1; return result; }
S T R I N G S
5555
Question 3: List all the anagrams possible for a string. No need to verify if it is an actual word in the dictionary or not. #include <iostream.h> #include <stdio.h> using namespace std; void findCombinations(string input, size_t len, vector< bool > occurred, char * anagram); void anagrams(string input){ vector< bool > occurred(input.length()); for (size_t index=0; index<input.length(); index++){ occurred[index] = true ; char * anagram = new char [occurred.size()]; *(anagram+0) = input[index]; findCombinations(input, 1,occurred, anagram) ; occurred[index]= false ; } } void findCombinations(string input, size_t len, vector< bool > occurred, char * anagram){ if (len == input.length()){ *(anagram + len) = '\0' ; cout<< "\n" <<anagram; } else { for (size_t index=0; index < input.length(); index++){ if (occurred[index] == false ){ *(anagram+len) = input[index]; occurred[index] = true ; findCombinations(input, len+1, occurred, anagram); occurred[index]= false ; } } } }
S T R I N G S
6666
Question 4: Find the longest palindrome substring from a block of text. For example: In the string, “This is a racecar program” has the longest substring “racecar”. string findPalindrome(string input){ if (input.length() < 2){ return input; } int lower; size_t upper, longest_palindrome= 0; size_t longest_lower=0,longest_upper=0,center=0, curr_length ; while (center < input.length()){ lower = upper= center; while ( lower >=0 && upper < input.length() && input[lower] == input[upper] ){ curr_length = upper-lower+1; if (curr_length > longest_palindrome){ longest_lower = lower; longest_upper = upper; longest_palindrome = curr_length; } lower = lower-1; upper = upper+1; } center = center +1; } return input.substr(longest_lower, longest_upper-longest_lower+1); }
Recommended