Interview Question


Country: Bangladesh
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
1
of 1 vote

What I understand from this code is if input is "I am indiaN" then output should be "I ma". Now to solve this problem we can think of following algorithm=>
scan the sentence and find words(word delimiter is white space)
For each word check last character is capital or not, if yes then remove the word otherwise reverse characters of the word.
repeat the process until all words are completed

- sachinagarwala251 November 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Perform in-place reversal of the original string.
e.g. "How arE you doinG today" => "yadot Gniod uoy Era woH"
Note that the words to be removed now have their first character in upper case.
2. Scan the resulting reversed string and reverse each term (word) in place, while
skipping those starting with upper case.
At this point, you will need to store the position of the last delimiter
encountered so far, because as soon as you skip any word starting with the
upper case, you will need to move it (copy over) the next valid word to the
position following the last encountered whitespace (space in this example)

The above example will result in the following string "today you How"

- ashot madatyan November 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Friend, i think u dint read the question, we have to reverse Chars of words also

- King November 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I some how missed that part - the "reverse the characters of each word". The problem is now even easier with my algo - at step #2, just do not do the in-place reversal of each word. Thanks for noting that, pal.

- ashot madatyan November 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't understand the reason behind reversing the entire string as you mentioned as the first step of your algortham, don't you think that is an extra step. Please explain

- Sudheer December 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming words are separated by spaces and there is only letters (no digits and so on) solution in scala:

"FooO bar baZ bow".split(" ").filter(word => word.last >= 'a').map(_.reverse)

- Anonymous November 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseWords {
	public static String removeIfEndsWithCapitalAndReverseOther(String query)
	{
		String[] words = query.split(" ");
		String result = "";
		for(int i=0; i<words.length; i++)
		{
			if(!isCapital(words[i].charAt(words[i].length()-1)))
			{result = result + " "+ reverse(words[i]);}
		}
		return result;
	}
	public static boolean isCapital(char c)
	{
		return (c>='A' && c<='Z');
	}
	public static String reverse(String word)
	{
		String result="";
		for(int i=word.length()-1; i>=0;i--)
		{
			result = result+ word.charAt(i);
		}
		return result;
	}
	public static void main(String[] args)
	{
		String query = "My attitude dependS on you";
		String result = removeIfEndsWithCapitalAndReverseOther(query);
		System.out.println(result);
	}
}

- Pre8y December 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.test.me;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class FindCaps {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

BufferedReader bufread = new BufferedReader(new InputStreamReader(System.in));
try {
String inp = bufread.readLine();
String[] str = inp.split(" ");
ArrayList<String> strArray = new ArrayList<String>();
ArrayList<String> newStrArray = new ArrayList<String>();
for(int i=0;i<str.length;i++){
char c = str[i].charAt(str[i].length()-1);
if(c>= 'A' && c<='Z'){
strArray.add(str[i]);
// System.out.print(str[i]);
}
}
System.out.print(strArray.size());
for(int i=strArray.size()-1;i>=0;i--){
String s = new StringBuffer(strArray.get(i)).reverse().toString();
newStrArray.add(s);
}
for(int k=0;k<newStrArray.size();k++){
System.out.print(newStrArray.get(k));
System.out.print("\t");
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}

- SkullCrusher February 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
public class Captal1 {
public static void main(String[] args) {
String s;
String n;
char c;
char ch[]=new char[10];
int l;
Scanner sc=new Scanner(System.in);
System.out.print("enter the given string");
s=sc.nextLine();
StringTokenizer st=new StringTokenizer(s," ");
while(st.hasMoreElements())
{
n=st.nextElement().toString();
//System.out.print(n);
l=n.length();
c=n.charAt(l-1);
//System.out.print(c);
if(!Character.isUpperCase(c))
{
ch=n.toCharArray();
for(int i=l-1;i>=0;i--)
System.out.print(ch[i]);
System.out.print(" ");
}
}


}

}
sample o/p:enter the given stringhello ram raki kittU keerthi
olleh mar ikar ihtreek

- ram97 March 05, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More