Epic Systems Interview Question for Software Engineer / Developers






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

public class tryex{
public static void main(String[] args){


String t = stringReplace("A boy is playin in a garden");
System.out.println(t);
}
static String stringReplace(String str) 
{
    String regex = " ";
    String str1;
    String str2;
    StringBuilder builder = new StringBuilder();
    String[] parts = str.split(regex);
    for(int i=0; i<parts.length; i++)
    {
     if(parts[i].length()>=4 && parts[i].length()%2==0)
     {
     str1 = parts[i].substring(0, parts[i].length()/2);
     str2 = parts[i].substring(parts[i].length()/2,parts[i].length());
     builder.append(str1);
     builder.append(" ");
     builder.append(str2);
     }
        else 
            builder.append(parts[i]);
        if (i<parts.length-1)
            builder.append(" ");
    }
   
    return builder.toString();
}
}

- rv April 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

In the question, "can't" will be separated to "ca n't", but it seems like your code doesn't do that.

- Park April 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Taking care of string with odd lenght and even length greater than 4

public class ReplaceWordWithSpaceLengthGreaterthan4 {

    public static void main(String[] args){
        String strinput = "A persons cant walk in this street";
        //System.out.println(replace(strinput));
        String t = StringReplace(strinput);
        System.out.println(t);
    }

    public static String StringReplace(String str) {
    String temp = "";
    String regex = " ";
    String[] words = str.split(regex);
    StringBuffer sb = new StringBuffer();
    for(int i=0;i< words.length;i++) {
        int len = words[i].length()/2;
        if(words[i].length() >= 4 && words[i].length()%2 == 0){
            sb.append(words[i].substring(0, len));
            sb.append(' ');
            sb.append(words[i].substring(len, words[i].length()));
        }else if (words[i].length() >= 4  && words[i].length() % 2 == 1){
            sb.append(words[i].substring(0, len));
            sb.append(' ');
            sb.append(words[i].substring(len+1, words[i].length()));
        }else {
            sb.append(words[i]);
        }
        sb.append(' ');
    }

    return sb.toString();
    }

- silvimasss February 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Walk the string and construct a second string, by keeping in mind to add space between 2 parts of the word.

- Anonymous April 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the intent was to construct the string inplace. e.g assume the character array has enough storage space at the back.

- Anonymous April 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi,

Is this a question of a test in on-site interview ?

- silence April 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if person is 'per son' then why is street 'stre et' and not 'str eet'? is that a typo?

- puzzled April 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It should be a typo

- Anonymous June 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

btw @rv: your douchebag solution doesn't consider {' , . - digits} in words.

- puzzled April 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Correct answer to the problem is

package b;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringReplace {
	public static void main(String[] args){
		String s = "A person can't walk in this street";
		String op = "";
		Scanner in = new Scanner(s);
		while(in.hasNext()){
			String token = in.next();
			Pattern p = Pattern.compile("\\W");
			Matcher m = p.matcher(token);
			int count = 0;
			while(m.find()){
				count++;
			}
			int trueLength = token.length() - count;
			
			if(trueLength >= 4 && trueLength % 2 ==0){
				String t1 = token.substring(0,trueLength / 2);
				String t2 = token.substring(trueLength /2);
				op += t1.concat(" ").concat(t2).concat(" ");
			}
			else{
				op += token.concat(" ");
			}
		}
		System.out.println(op);
	}
}

- anonymous April 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's not working... You assume that all the special char is located in second half. check this case d'ont => d'o nt

- SuperK December 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I dont think anybody noticed that in street the space is after the 'e' and not 'r'...length/2 would give you the wrong answer

- einstein010 April 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
string s = "Away person can't walk in this street";
string temp = "";
string rem = s;
int start = 0;
for(int i = 0; i < rem.length();i++)
{
if(rem[i] == ' ')
{
int diff = i - start;
cout<<diff<<endl;
if((diff%2 == 0)&&(diff>=4))
{
temp = temp + rem.substr(0,start+diff/2) +" "+
rem.substr(start + (diff/2),(diff/2));
rem = rem.substr(i,rem.length());
i = 0;
}
start = i+1;
}
}

int i = 1;
start = i;
while(rem[i]!= '\0')
i++;
int diff = i - start;
if((diff%2 == 0)&&(diff>=4))
{
temp = temp + rem.substr(0,start+diff/2)+ " " + rem.substr(start+(diff/2),(diff/2));
rem = rem.substr(i,rem.length());
}
cout<<temp<<endl;
cout<<rem;





getch();
return 0;
}

- knoweth October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args)
{
String input = "A person can't walk in this street";
String[] words = input.split(" ");
StringBuffer sbf = new StringBuffer();
char[] tempStr;
int counter=0;

for(int i=0; i<words.length; i++)
{
tempStr = words[i].toCharArray();
counter=0;

for(int j=0; j<tempStr.length; j++)
{
if((tempStr[j]>='a' && tempStr[j]<='z') || (tempStr[j]>='A' && tempStr[j]<='Z'))
{
counter++;
}
}
if(counter>=4 && counter%2==0)
{
for(int j=0; j<tempStr.length; j++)
{
sbf.append(tempStr[j]);
if(j==(counter/2)-1)
sbf.append(" ");
}
}
else
{
sbf.append(tempStr);
}
sbf.append(" ");
}
System.out.println(sbf.toString());

}

- Steve November 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.StringTokenizer;


public class AddSpaceBetweenLetters {

/**
* Given a string.Replace the words whose lengt>=4 and is even,with a space between
* the two equal halves of the word.consider only alphabets for finding the eveness
* of the word
I/P "A person can't walk in this street"
O/P "A per son ca n't wa lk in th is stre et"
13
*/

public static void main(String[] args) {

String str="A person can't walk in this street";
String temp="";
String newStr="";
int wordSize;

System.out.println("*******OLD******"+str);

StringTokenizer stokens=new StringTokenizer(str);
while(stokens.hasMoreTokens())
{

temp=stokens.nextToken();
wordSize=temp.length();
char[] c=new char[wordSize+1];

if(wordSize>=4&&(wordSize%2)==0)
{
for(int i=0;i<wordSize;i++)
{
if(i<wordSize/2)
{
c[i]=temp.charAt(i);
}
if(i==wordSize/2)
{
c[i]=' ';
}
if(i>wordSize/2&&i<=wordSize)
{
c[i]=temp.charAt(i-1);
}

c[wordSize]=temp.charAt(wordSize-1);

}

String t=new String(c);
newStr=newStr+" "+t;

}
else
{
newStr+=" "+temp;
}
}

System.out.println("******NEW******"+newStr);
}

}

- Neek December 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey20598" class="run-this">import java.io.*;
import java.util.*;

public class ReplaceWord {

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
br.close();
String output = "";
StringTokenizer st = new StringTokenizer(input);
while (st.hasMoreTokens()) {
output += getConvertString(st.nextToken());
if (st.hasMoreTokens()) {
output += " ";
}
}
System.out.println(output);
}

public static String getConvertString(String input) {
String output = "";
int length = 0;
for (int i = 0; i < input.length(); ++i) {
char current = input.charAt(i);
if (current != ',' && current != '!' && current != '"' && current != '?' && current != '\'') {
++length;
}
}
if (length % 2 == 0 && length >= 4) {
int size = length / 2;
int setted = 0;
for (int i = 0; i < input.length(); ++i) {
char current = input.charAt(i);
if (setted < size) {
if (current != ',' && current != '!' && current != '"' && current != '?' && current != '\'') {
++setted;
}
}
output += current;
if (setted == size) {
output += " ";
++setted;
}
}
return output;
}
return input;
}
}
</pre><pre title="CodeMonkey20598" input="yes">
</pre>

- SuperK December 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

int count_only_alphabet(string s)
{
	int cnt=0;
	for (int i=0;i<s.length();i++)
		if (('a'<=s[i] && s[i]<='z') || ('A'<=s[i] && s[i]<='Z'))
			cnt++;
	return cnt;
}

string split(string s, int count)
{
	int i=0,b=0;
	while (b<s.length() && i<count/2)
	{
		if (('a'<=s[b] && s[b]<='z') || ('A'<=s[b] && s[b]<='Z'))
			i++;
		b++;
	}
	s.replace(b,0," ");
	return s;
}

string replace_simple(string str)
{
	string temp="";
	int space=str.find(' ');
	int i=0;
	string word=str.substr(i,space-i);
	while (word!="")
	{
		int count=count_only_alphabet(word);
		if (count>=4 && count%2==0)
		{
			word=split(word,count);
		}
		temp=temp+" "+word;
		if(space==string::npos){break;}
		i=space+1;
		space=str.find(' ',i);
		word=str.substr(i,space-i);
	}
	return temp;
}

int main()
{
	cout<< "Enter the string \n";
	string s;
	getline(cin,s);
	cout<<replace_simple(s);
}

- priyanshu October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include <string>
#include <list>
using namespace std;
int main()
{
    string s,temp;
    int counter=0;
    getline ( cin,s );
    int k=0;
    list<string> l;
    list<string> z;
    list<string>::iterator itr,ztr;
    for ( int i=0; i<s.length(); i++ )
    {
        if ( s[i]==' ' )
        {
            l.push_back ( s.substr ( k,i-k ) );
            k=i+1;
        }
    }
    l.push_back ( s.substr ( k,s.length()-k ) );

    ztr=z.begin();
    for ( itr=l.begin(); itr!=l.end(); itr++ )
    {

        temp=*itr;
        counter=0;
        for ( int  w=0; w<temp.size(); w++ )
        {
            if ( ( temp[w]>='a'&&temp[w]<='z' ) || ( temp[w]>='A'&&temp[w]<='B' ) ) {
                counter++;
            }
        }
        if ( counter >=4 &&  counter %2==0 )
        {

            z.push_back ( temp.substr ( 0,counter/2 ) ) ;
            z.push_back ( temp.substr ( counter /2 ,counter ) ) ;

        }
        else {
            z.push_back ( temp ) ;

        }

    }
    for ( ztr=z.begin(); ztr!=z.end(); ztr++ )
    {
        cout<< *ztr<<" ";
    }
    getchar();
}

- gogetit June 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please correct the typo before executing the code

if ( ( temp[w]>='a'&&temp[w]<='z' ) || ( temp[w]>='A'&&temp[w]<='Z' ) )
{
counter++;
}

- gogetit June 14, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReplaceString1 {
	/**
	 * Given a string.Replace the words whose lengt>=4 and 
	 * is even,with a space between the two equal halves of the word.
	 * consider only alphabets for finding the eveness of the word 
		I/P "A person can't walk in this street" 
		O/P "A per son ca n't wa lk in th is stre et"
	 * 
	 */

	public static void main(String[] args) {
		String str = "A person can''t walk in this street";
		String mStr = replace(str);
		System.out.println(mStr);

	}

	private static String replace(String str) {
		StringBuffer sb = new StringBuffer();
		String[] tokens = str.split(" ");
		for(String s: tokens) System.out.println(s);
		for(String s: tokens){
			if(len(s)<4) {
				sb.append(s);
				sb.append(" ");
			}
			else{
				if(len(s)%2==0){
					sb.append(s.substring(0,len(s)/2));
					sb.append(" ");
					sb.append(s.substring(len(s)/2) + " ");
					
				}
				else{
					sb.append(s + " ");
				}
			}			
		}
		return sb.toString();
	}
	
	public static int len(String s){
		int totalLen = s.length();
		int count = 0;
		for(char c:s.toCharArray()){
			if(c== '\'') count++;
		}
		return totalLen - count;
	}

}

- crazykani November 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if __name__ == "__main__":
	inputStr = "A person can't walk in this street"
	strList = inputStr.split(" ")
	print strList
	for x in strList:
		if (len(x) >= 4) and ((len(x)%2) == 0):
			strList[strList.index(x)] = x[0:len(x)/2] + " " + x[len(x)/2:len(x)+1]
	print strList
	outputStr = ''
	for x in strList:
		outputStr += x
		if strList.index(x) != (len(strList)-1):
			outputStr += " "
	print outputStr

- natch January 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package practice;
import java.util.*;
public class SplitString {
	public String Split(String s){
		String regex=" ";
		s.trim();
		String str1,str2;
		String[] part=s.split(regex);
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<part.length;i++){
			
			char[] temp=part[i].toCharArray();
			int counter=0; 

			for(int j=0; j<temp.length; j++) 
			{ 
			if((temp[j]>='a' && temp[j]<='z') || (temp[j]>='A' && temp[j]<='Z')) 
			{ 
			counter++; 
			} 
			} 
			if(counter>=4 && counter%2==0) 
			{ 
			for(int j=0; j<temp.length; j++) 
			{ 
			sb.append(temp[j]); 
			if(j==(counter/2)-1) 
			sb.append(" "); 
			} 
			} 
			else 
			{ 
			sb.append(temp); 
			} 
			sb.append(" "); 
			} 
		
		return sb.toString();
	}
	public static void main(String[] args){
		SplitString s1=new SplitString();
		String result=s1.Split("A person can't walk in this street");
		System.out.print(result);
	}

}

- Anonymous January 31, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package practice;
import java.util.*;
public class SplitString {
	public String Split(String s){
		String regex=" ";
		s.trim();
		String str1,str2;
		String[] part=s.split(regex);
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<part.length;i++){
			
			char[] temp=part[i].toCharArray();
			int counter=0; 

			for(int j=0; j<temp.length; j++) 
			{ 
			if((temp[j]>='a' && temp[j]<='z') || (temp[j]>='A' && temp[j]<='Z')) 
			{ 
			counter++; 
			} 
			} 
			if(counter>=4 && counter%2==0) 
			{ 
			for(int j=0; j<temp.length; j++) 
			{ 
			sb.append(temp[j]); 
			if(j==(counter/2)-1) 
			sb.append(" "); 
			} 
			} 
			else 
			{ 
			sb.append(temp); 
			} 
			sb.append(" "); 
			} 
		
		return sb.toString();
	}
	public static void main(String[] args){
		SplitString s1=new SplitString();
		String result=s1.Split("A person can't walk in this street");
		System.out.print(result);
	}

}

- Anonymous January 31, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I have a doubt!
suppose the word is 'alphabet'
The output should be 'alph abet' or it should be 'al ph ab et'!!

thanx

- rv April 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you should output "alph abet" since it said "with a space between the two equal halves of the word"

- x.c April 12, 2010 | Flag


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