## Credit Suisse Interview Question for Analysts

• 0

Country: India
Interview Type: Written Test

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

List traversal, O(n) time, saving the largest you see.

``````// C#
public string FindLongest(string[] s)
{
if (s == null || s.Length == 0)
{
return ""; // Or throw, depending on interviewer's preference.  Always error check
}
string maxSeen = string.Empty;
for(int i = 0; i < s.Length; i++)
{
if(s[i].Length > maxSeen.Length)
{
maxSeen = s[i];
}
}
return maxSeen;
}``````

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

make two empty stack. Push all the string in a stack
suppose the stack name is s in which you want to push

for(i=1;i<size+1;i++) {
b=s.pop();
int k=b.compareTo(max);
if(k>0) {
max=b;
}
t.push(b);
}
System.out.print(max+" ");
for(i=1;i<size+1;i++) {
b=t.pop();
s.push(b);
}

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

What a bizarre solution.

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

import java.util.Arrays;
public class Expand {
public static void main(String args[])
{
String[] arr={"careercu","career" ,"dewdeals" , "online","filewrit","filewrit"};
int [] arr1=new int[arr.length];
int [] arr2=new int[arr.length];
for(int i=0;i<arr.length;i++)
{
arr1[i]=arr[i].length(); //making two arrays to grab the length of one of the string
arr2[i]=arr[i].length();
}
Arrays.sort(arr2);//to sort the array lengths
int x=arr2[arr1.length-1];
for(int j=0;j<arr1.length;j++)
{
if(x==arr1[j])
System.out.println(arr[j]);
}
}
}

this code perfectly works for any no os the strings

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

``````#include<conio.h>
#include<iostream.h>
#include<string.h>

void main()
{

int i,num,max,max_pos;
clrscr();
cout<<"enter the number of words you want to compare";
cin>>num;
char ch[10][50];
for(i=0; i<num;i++)
{
cout<<"enter string"<<i;
cin>>ch[i];
}

if(ch==NULL)
{
cout<<"error"; //or return();

}
else
{
max= strlen(ch[0]);
max_pos= 0;
for (i=1;i<num;i++)
{
if (max< strlen(ch[i]))
{
max= strlen(ch[i]);
max_pos=i;
}

}
cout<< "found element"<<ch[max_pos]<<"\t"<<max;
}
getch();
}``````

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

Yeah its right

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

program will crashed if entered more than 10 number of string.

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

You can do it in sublinear time by using a trie.

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

Can you provide the code for this? Remember you are given a String[], you cannot magically transform it into a trie. I believe that this would take O(nlog(a)) to construct where n is the size of the array and a is the average string size. Why not just do linear search?

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

``````scala> val xs = List("Google", "Microsoft", "Apple", "Amazon")
xs: List[java.lang.String] = List(Google, Microsoft, Apple, Amazon)

scala> xs.maxBy(_.length)
res1: java.lang.String = Microsoft``````

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

import java.util.HashMap;
import java.util.Map;

public class LargestString {
public static void main(String[] args) {
String[] stringTable = new String[] {"careercup", "career", "dewdeals", "onlineshopping", "onlineshoppin1"};

System.out.println("Result is:" + largestString(stringTable));

}

private static String largestString(String... stringTable) {
assert stringTable != null && stringTable.length > 0 : "Input cannot be null or empty table.";
Map<Integer, String> cache = new HashMap<Integer, String>(stringTable.length);
int l, maxLength = 0;
for(String s : stringTable) {
l = s.length();
cache.put(l, s);
maxLength = Math.max(maxLength, l);
}
return cache.get(maxLength);
}
}

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

Why do you need a HashMap? Why don't you just store one string and replace that string if you see a string with more characters as you traverse the array?

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

I found this one simpler

``````public class LargestString {
public static void main(String[] args) {
String[] arr={"careercu","career" ,"dewdeals" , "online","filewrit","filewrit"};
int maxLen=0;
for(String str:arr){
if(maxLen<str.length()){maxLen=str.length();}
}

for(String str:arr){
if(str.length()==maxLen){System.out.println(str);}
}

}``````

}

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

You're traversing the array twice, which is unnecessary. If there can be only one longest string then just store the longest string you see as you traverse the array. If there can be more than one longest string then keep a container of all the strings which have the most characters seen, emptying it if you see a string with more characters.

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

char* str[] = {"Career_Cup","Vinay","India","No"};
int maxLen = 0;
int index = -1;
for(int i = 0; i < sizeof(*str); ++i)
{
int cur_len = strlen(str[i]);
if( cur_len > maxLen)
{
maxLen = cur_len;
index = i;
}
}
cout<<"Max Len string is:"<<str[index];
}

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

I like the idea that you don't even need to store the longest string, you just need to store an index to that string. But:

1) char* str[] etc. needs to be const char* str[] to avoid warnings with my compiler. Anything between "" is implicitly a const char*.
2) I don't think you're using sizeof correctly. Dereferencing the pointer "str" will give you the first element in the array. So in other words sizeof(*str) is sizeof(char*). On my system the size of a pointer is 4 bytes. So in other words the code breaks if you add "Test test test test test test" to the end of the array, because that is the fifth element and we're only looping four times.
3) The code does work if you just use a constant for the size of the array which you can then use to loop through. Or a sensible C++ solution would be to use something like vector<string>, and then you don't have to worry about magic numbers.

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.

### 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.

### 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.

### 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.