Microsoft Interview Question
Software Engineer in TestsCountry: India
Interview Type: Written Test
Why thinking so hard?
HERE IS THE CONCEPT:
int FindNumOfA(char* sentence)
{
int cnt = 0;
if(tolower(sentence[0]) == 'a' && (sentence[1] == ' ' || sentence[1] == '\0')){
cnt ++;
}
int i = 1;
while( sentence[i] != '\0' ){
if( tolower(sentence[i]) == 'a'){
if(sentence[i - 1] == ' ' && sentence[i+1] == ' '){
cnt ++;
}
}
i++;
}
return cnt;
}
OUTPUT:
Eg: If a sentence is given as "I found an apple in a tree."
The output is : 1 (not 2)
I think we can just take the string as the input..... using String.split(" a ") , we get an array of size n, then there are (n-1) occurrences of "a"
int NumA(char *s)
{
char *length=s+strlen(s);
int flag=0,count=0,len=0;
char *b;
while(s<length)
{
if(*s!=' ')
{
b=s;
while(s<length && *s!=' ')
{
if(*s=='a')
{
flag=1;
}
len++;
s++;
}
if(flag==1 && len>2)
{count++;
flag=0;
len=0;
}
else if(flag==1)
{
flag=0;
}
}
}
return count;
}
String str="I found an apple in a tree";
String[] articles={"a","an","the"};
String[] inputString=str.split(" ");
char inputChar='a';
int wordCnt=0;
for(String s: inputString){
if(!s.equalsIgnoreCase(articles[0])&&
!s.equalsIgnoreCase(articles[1])&&
!s.equalsIgnoreCase(articles[2])){
if(s.length()>1){
for(int i=0;i<s.length();i++){
if(inputChar==s.charAt(i)){
wordCnt+=1;
break;
}
}
}
}
}
System.out.println("word count:"+wordCnt);
This works.
String str="I found an apple in a tree";
String[] articles={"a","an","the"};
String[] inputString=str.split(" ");
char inputChar='a';
int wordCnt=0;
for(String s: inputString){
if(!s.equalsIgnoreCase(articles[0])&&
!s.equalsIgnoreCase(articles[1])&&
!s.equalsIgnoreCase(articles[2])){
if(s.length()>1){
for(int i=0;i<s.length();i++){
if(inputChar==s.charAt(i)){
wordCnt+=1;
break;
}
}
}
}
}
System.out.println("word count:"+wordCnt);
I would suggest to use a window to processing the sentence plus a running count.
1. define 3 variable for the window and a pointer, say c0, c1, c2 and p. initialize them to -1.
2. start 1st character, p=1 and assign c2=s[p] and c1=s[p-1] and c0 = s[p-2] make sure the indexes are greater than zero before assignment.
3. if c1 equal to "a" and c0 = -1 or non-character and c2= -1 or non-charater, then add 1 to running count, and increase p by 3 and go back step 2.
4. if c1 not equal to "a" and c1 is character
a. and if c2 is non-character, then increase p by 2 and go back step 2.
b. otherwise, increase p by 3 and go back step 2.
5. do this until end of the sentence
please share and help to verify my solution. thx.
Aside from the normal case " a ", you also need to check for the boundary cases which include when the "a" is in the beginning of the string, the only letter in the string and at the end. This is a possible solution:
#include <iostream>
#include <string>
using namespace std;
int numWords(const string & word)
{
int result = 0;
if (word.at(0) == 'a' && word.size() == 1)
return 1;
for (int i = 0; i < word.size() - 1; ++i)
{
if (i == 0 && word.at(i) == 'a' && word.at(i + 1) == ' ')
++result;
else if (i == word.size() - 2 && word.at(i) == ' ' && word.at(i + 1) == 'a')
++result;
else if (word.at(i) == ' ' && word.at(i + 1) == 'a' && word.at(i + 2) == ' ')
++result;
}
return result;
}
int main()
{
string word = "a How are you this is a test of a possible input a";
cout << numWords(word) << endl;
return 0;
}
The output in this case is 4
class Program
{
public static int FindWordsContainingLetter(string sentence, char chr)
{
char[] sentArray = sentence.ToCharArray();
int j =0;
int cnt = 0;
for (int i = 0; i < sentArray.Length; i++)
{
if (sentArray[i] != ' ')
{
i++;
}
else if (sentArray[i] == ' ' && i - j > 1) //ignore single letters
{
if (FindCharHelper(sentArray, j, i - 1, chr)) //i is sitting at space so decrement before passing
{
cnt++;
}
}
}
return cnt;
}
public static bool FindCharHelper(char[] wordArray, int strt, int end, char chr)
{
bool found = false;
for (int i = strt; i < end; i++)
{
if (wordArray[i] == chr)
{
found = true;
break;
}
}
return found;
}
static void Main(string[] args)
{
FindWordsContainingLetter("Does this string contain words with letter i", 'i');
}
}
}
public static int countAWords(String st) {
String[] strarr=st.split("\\s");
int count=0;
for(int i=0;i<strarr.length;i++) {
if(strarr[i].equalsIgnoreCase("a")) {
count++;
}
else {
continue;
}
}
return count;
}
We can separate the words and then check if its an 'a' word. We will have to parse the sentence atleast once.
public static String CountAs(String words) {
char[] letters = words.toCharArray();
int start = 0;
int end = 0;
int count = 0;
while (end < letters.length) {
if ((letters[end] == ' ' || letters[end] == letters.length - 1)) {
if (chkForA(letters,start,end-1)) {
count++;
start = end + 1;
} else {
start = end + 1;
}
}
end++;
}
if (chkForA(letters,start,end-1)) {
count++;
}
return count;
}
private static boolean chkForA(char[] letters, int start, int i) {
if((start == i) && letters[start] == 'a')
return true;
else
return false;
}
Takes the sentence and the searched word (key) and returns the count of the key.
public int findFrequency(String sentence, String key) {
int count = 0;
for (String word: sentence.split("\\W+")) {
if (key.equals(key)) count++;
}
return count;
}
I dont think we are asked to find the frequency of a given word in a sentence here.
There needs to be a clarification on what is the meaning of number of 'a' words in the question.
@oOZz
your basic idea kind of works, but I think it is overkill to store all the words. Instead of a hashmap, just use one counter which increments whenever an 'a' word occurs. Also, not sure if 'a' word means 'a' or any word starting with 'a' and length > 1. If it is the latter, your idea doesnt work, since I dont think there is a way to find out which of the keys start with 'a' unless we use an iterator (which, btw is a hashset).
contd. from above comment. Looks like we are expected to find only strings with value "a". oOZz's approach will work.
@anon it can't be the latter, because then you have "an" and "apple" and the output will be 2.
private static int HowManyA(string text)
{
if (String.IsNullOrEmpty(text))
return 0;
var word = "";
var count = 0;
foreach (var t in text)
{
if (t != ' ')
word += t;
else
{
if (word == "a")
count++;
word = "";
}
}
return count;
}
you are right. here is a modified version
private static int HowManyA(string text)
{
if (String.IsNullOrEmpty(text))
return 0;
var word = "";
var count = 0;
for (int i = 0; i < text.Length; i++)
{
if (i == text.Length-1 && text[i] == 'a' && word == "")
{
count++;
continue;
}
var t = text[i];
if (t != ' ')
word += t;
else
{
if (word == "a")
count++;
word = "";
}
}
return count;
}
In python
- turan.emre June 08, 2013