Credit Suisse Interview Question
AnalystsCountry: India
Interview Type: Written Test
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);
}
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
#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();
}
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);
}
}
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);}
}
}
}
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.
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];
}
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.
List traversal, O(n) time, saving the largest you see.
- patrick August 31, 2012