Amazon Interview Question
SDE1sCountry: India
Interview Type: Written Test
public void main()
{
ArrayList <String> arrayList = new ArrayList<String>();
arrayList.add("a,b,c,d,e,f");
arrayList.add("1,2,3,4");
arrayList.add("w,x,y,z");
String result = new String();
for(String getS : checkBeforeFinal(arrayList)){
result = result + getS + "\n";
}
System.out.print(result);
}
private ArrayList<String> checkBeforeFinal(ArrayList<String> input){
ArrayList<String> finalRetVal = new ArrayList<String>();
String retVal = null;
String charBeforeComma = null;
for(int i=0; i< input.size(); i++){
String getStr = input.get(i);
for(int j=0; j < getStr.length(); j++){
char getChar = getStr.charAt(j);
String compare = String.valueOf(getChar);
if(!compare.equals(",")){
charBeforeComma = String.valueOf(getChar);
}else{ // the char is comma
// reset charBeforeComma
retVal = charBeforeComma;
charBeforeComma = null;
}
} // end of for j
finalRetVal.add(retVal);
} // end of for i
return finalRetVal;
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Challenge3 {
public static void main(String[] args) {
try{
File f1=new File("test.txt");
FileReader freader=new FileReader(f1);
BufferedReader buffer=new BufferedReader(freader);
String value;
while((value=buffer.readLine())!=null){
String[] charArry=value.split(",");
System.out.println(charArry[charArry.length-2]);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
Output:
- Felipe Cerqueira February 08, 2017