Citigroup Interview Question for Java Developers


Country: India




Comment hidden because of low score. Click to expand.
4
of 6 vote

public class StringLengthTest {
	
	public static void main(String[] args){
		String str = "abcdef";
		char[] elements = str.toCharArray();
		int count=0;
		for(char c : elements){
			count++;
		}
		System.out.println(count);
	}

}

- Anonymous October 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

impressive!

- java.interviews.questions October 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice

- S O U N D W A V E October 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

better than exception use

- Kenneth October 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

My try in Java

public class StrLen {

	public static void main(String[] args) {
		System.out.println(strlen("abcde"));
	}
		
	static int strlen(String str) {

		if (str != null) {
			for(int len = 0;; len++) {
				try {
					str.charAt(len);
				}
				catch (IndexOutOfBoundsException e) {
					return len;
				}
			}
		}
		
		return 0;
	}
}

- SJ Park October 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

int strlen(String a) {
if (a == null) return 0;
int i = 0;
try {
while (a.charAt(i) != -1) i++;
}
catch (IndexOutOfBoundsException e) {
return i;
}
return i;
}

- cgj October 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
 * How could you make sure that thread A ,B and C run sequentially without using join method?
 * 
 * @author manoj.sharma
 * @source Barclays
 */

public class ThreadJoiningWithoutJoin {

	public static void main(String[] args) {
		final JoinTask task = new JoinTask();
		
		Thread A = new Thread(){
			public void run(){
				task.doJob(1, "JOB A DONE...");
			}
		};
		
		Thread B = new Thread(){
			public void run(){
				task.doJob(2, "JOB B DONE...");
			}
		};
		
		Thread C = new Thread(){
			public void run(){
				task.doJob(3, "JOB C DONE...");
			}
		};

		C.start();
		B.start();
		A.start();
		
	}

}

// Shared Class or Object 
class JoinTask {
	
	private int currentRank = 1;
	
	public void doJob(int rank, String msg) {
		synchronized(this) {
			while (rank != currentRank) {
				try {wait();}catch(InterruptedException ex) {ex.printStackTrace();};
			}
			System.out.println("Job:" + currentRank + " : " + msg );
			currentRank++;
			notifyAll();
		}
	}
}

- Manoj Sharma October 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It does not need to check all positions.

Check char at position 2 then 4 then 8 and so on until caught exception.

public static int len(String str){
		if(str==null)
			return 0;
		else{
			int index=2;
			try {
				while (true){
					str.charAt(index); 
					index *= 2;
				}
			} catch (IndexOutOfBoundsException e) {
				return checkSize(str,index/2,index);
			}
		}
	}

then call recursive method which returns the position of last char.

public static int checkSize(String str, int l, int r){
		if(l+1==r)
			return r;
		else {
			int index=(l+r)/2;
			try{
				str.charAt(index);
				return checkSize(str,index,r);
			}
			catch (IndexOutOfBoundsException e) {
				return checkSize(str,l,index);
			}
		}
	}

- ehsan October 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args){
		String s="string jelly";
		int l=getLength(s);
		System.out.print(l);
	}
	
	static int getLength(String s){
		char[] ch=s.toCharArray();
		int len=0;
		for(char c:ch){
			len++;
		}
		return len;
	}

- shashi October 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StringTest {

	public static void main(String args[]){
		String s = new String("test213");
		try {
			int count = (Integer)getFieldValue(s, "count");
			int offset = (Integer)getFieldValue(s, "offset");
			System.out.println("Size :"+(count-offset));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static Object getFieldValue(String s,String fieldName) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
		Object chars = null;
		Field fieldValue = String.class.getDeclaredField(fieldName);
		fieldValue.setAccessible(true);
		chars = fieldValue.get(s);
		return chars;
	}

}

Use Reflection to get values of count and offset.

- welcometotalk December 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int getLength(String src){
		int len = 0;
		String temp= src;
		try{
			while(null != temp){
				temp = src.substring(len);
				System.out.println(temp+" : "+len);
				len++;
			}
		}catch(StringIndexOutOfBoundsException se){
			
		}
		
		return len-1;
	}

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

a better approach:

String input = "HelloWorld";
		System.out.println(input.lastIndexOf(""));

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

public class StringLength2{
static String name="BHAVANI";
static int cnt=0;
public static void main(String[] args){
while(true){
try{
if(Character.isLetter(name.charAt(cnt))){
cnt++;
}}catch(Exception E){
System.out.println("length is : " + cnt);
break;
}}}
}

- kiransaichandra June 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String input = "abcde ";
int n = input.indexOf(' ');
System.out.println("result: " + n);

- Anonymous September 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

You can read bytes from String. It will return byte array. Now you can get the length of it, which is length of String.

Source code is given below :

public int getSize(String str) {
		byte[] arr = str.getBytes();
		int length = arr.length;
		System.out.println("Length = " + length);
		return length;
	}

- Manoj Sharma October 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Good idea! But I think..
Any length or size method is not allowed even for byte array according to the question.

- SJ Park October 16, 2013 | 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