Interview Question for Java Developers


Country: India
Interview Type: Written Test




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

package com.vidhu.string;
import java.util.ArrayList;
/*
This is for spliting a string without using split method. Also this split method does not take regex as input.
It can take at max two chars, in case of two chars it expects first char as \ for special chars in string.
 */
public class SplitString {

    String S;

    public SplitString(String s){
        S = s;
    }
    public String[] split(String regex){
        char ch;
        if(regex==null||regex.length()>2)
            return null;
        else if(regex.length()==2&&regex.charAt(0)=='\\')
            ch = regex.charAt(1);
        else
            ch = regex.charAt(0);

        String[] result=null;
        ArrayList<String> stringList = new ArrayList<String>();
        StringBuilder str= new StringBuilder("");

        for(int i=0;i<this.S.length();i++){
            if(this.S.charAt(i)==ch){
                stringList.add(str.toString());
                str = new StringBuilder("");
            }
            else
                str.append(this.S.charAt(i));
        }
        stringList.add(str.toString());
        result = new String[stringList.size()];
        return stringList.toArray(result);
    }

    public static void main(String args[]){
        SplitString obj = new SplitString("?vidhu?shekhar");

        System.out.println(obj.split("?").length);
        for(String s:obj.split("?"))
            System.out.println(s);
        System.out.println(",vidhu,shekhar".split(",").length);
    }

}

- Anonymous October 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.List;



public class SplitWithoutFunction {
	
	public static void main(String[] args) {
		
		String test = "Shuhail Kadavath Abc";
		String[] st = split(test, " ");
		for(String s : st)
			System.out.println(s);
	}
	
	
	public static String[] split(String s,String pat) {
		
		List<String> str = new ArrayList<>();
		
		int st=0;
		for(int i=0;i<s.length();i++) {
			
			if((""+s.charAt(i)).equals(pat)) {
				
				str.add(s.substring(st,i));
				st=i+1;
			}
		}
		
		if(st<s.length())
			str.add(s.substring(st,s.length()));
		String[] op = new String[str.size()];
		return (String[]) str.toArray(op);
	}

}

- shukad333 October 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do you think it'll work when pat is a string with more than 1 characters?

- Varun October 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static ArrayList<String> mySplit(String regex,String source){
		ArrayList<String> arr = new ArrayList<String>();
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(source);
		int left = 0;
		while(matcher.find()){
			System.out.println(source.substring(left,matcher.start()));
			left = matcher.end();
		}
		if(left< source.length() - 1)
		{
			System.out.println(source.substring(left,source.length()));
		}
		
		return arr;
	}

- Lina October 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test {

public static void main(String... args){
Test t1 = new Test();
String[] str = t1.mySplit("Hello World", " ");
for(String string:str){
System.out.println(string);
}

}
public String[] mySplit(String string, String ch){
List<String> output = new ArrayList<String>();
while(string != null){
int startIndex = 0;
int lastIndex = string.indexOf(ch);

if(lastIndex == -1){
if(string.length() > 0){
lastIndex = string.length();
} else {
break;
}
}
output.add(string.substring(startIndex,lastIndex));
if(lastIndex == string.length()){
string = null;
}else{
string = string.substring(lastIndex+1);
}

}
return output.toArray(new String[output.size()]);
}
}

- Anupam Govind October 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static string name(String name){
for(int i = 0;i < name.length();i++){
string one = name.substring(0,i);
string two = name.substring(i,name.length());
if(DICTIONARY.contains(one) && DICTIONARY.contains(two)){
return one + " " + two;
}
return name;

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

public static String[] splitStr(String s, String regex){
String[] result = new String[s.length()];
int index = 0;
int regexLength = regex.length();
int firstIndex = 0;
int lastIndex = 0;
for(int i = 0; i <= s.length() - regexLength; i++){
boolean match = true;
int t = 0;
for(int j = i; j < (i + regexLength); j++){
if(s.charAt(j) != regex.charAt(t)){
match = false;
}
t++;
}
if(match){
lastIndex = i - 1;
if(lastIndex >= firstIndex){
result[index++] = s.substring(firstIndex, lastIndex+1);
}
firstIndex = i + regexLength;
}
else{
if(i == s.length() - regexLength){
result[index++] = s.substring(firstIndex,s.length());
}
}
}

return result;
}

- adahui099 March 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.StringTokenizer;
class Splitting
{
	public static void main(String args[])
	{
	String str="This is Prasad";
	StringTokenizer tokenizer=new StringTokenizer(str);
	while(str.hasMoreTokens())
	{
	System.out.println(str.nextToken());
	}
	}
}

- pulipati.pulipati.prasad4 April 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void printTokenized(String text) {
		//String text = "   This is   a tokenized String";
		List<String> arr = new ArrayList<String>();
		StringBuilder sb = new StringBuilder();
		char delimeter = ' ';
		for(int i=0; i<text.length(); i++)
		{
			char ch = text.charAt(i);
			if(ch == delimeter)
			{
				if( sb.length()>0){
					arr.add(sb.toString());
					sb = new StringBuilder();
				}
			}
			else{
				sb.append(ch);
			}
		}
		System.out.println(arr);
		

	}

- Swati Gour April 21, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void printTokenized(String text) {
		//String text = "   This is   a tokenized String";
		List<String> arr = new ArrayList<String>();
		StringBuilder sb = new StringBuilder();
		char delimeter = ' ';
		for(int i=0; i<text.length(); i++)
		{
			char ch = text.charAt(i);
			if(ch == delimeter)
			{
				if( sb.length()>0){
					arr.add(sb.toString());
					sb = new StringBuilder();
				}
			}
			else{
				sb.append(ch);
			}
		}
		System.out.println(arr);
		

	}

- Swati Gour April 21, 2018 | 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