Amazon Interview Question
Java DevelopersCountry: United States
Interview Type: Written Test
import java.io.*;
import java.util.*;
public class Question_1 {
public static void main(String[] args) {
String Ifile="\\taxonomy.en-US.txt"; //input file
String Outputfile="\\Output.txt"; //output file
BufferedReader br;
FileReader fr;
File Ofile;
try
{
fr=new FileReader(Ifile);
br=new BufferedReader(fr);
Ofile=new java.io.File(Outputfile);
FileOutputStream fos=new FileOutputStream(Ofile);
PrintWriter pw=new PrintWriter(fos);
Set<String> hs=new HashSet<String>();
int StringLen=0;
String currentLine;
LinkedHashMap<String,Integer> map= new LinkedHashMap<String, Integer>();
while((currentLine= br.readLine()) != null )
{
currentLine += " >";
currentLine=currentLine.trim();
StringLen=currentLine.length();
while(StringLen>0)
{
int n=currentLine.indexOf(">", StringLen-1);
String sub=currentLine.substring(0, n);
hs.add(sub);
int ln=sub.lastIndexOf(">", n-2);
StringLen=ln;
}
for(String strcontain:hs)
{
if(map.containsKey(strcontain))
{
map.put(strcontain, map.get(strcontain)+1);
}
else
{
map.put(strcontain, 1);
}
}
hs.clear();
}
for (Map.Entry m:map.entrySet())
{
pw.println(m.getKey()+"="+m.getValue());
}
System.out.println("Output File Write Completed");
pw.flush();
pw.close();
fos.close();
br.close();
fr.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOExcption;
import java.util.*;
class WordCount {
private static final String FILENAME = "/Users/MacOs/Desktop/taxonomy.en-US.txt";
public static void main(String[] args) {
BufferedReader br = null;
FileReader fr = null;
Map <String, Integer> hm= new HashMap<String, Integer>();
int count =0;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
int i =0;
String s = "";
while(i<sCurrentLine.length()){
while(i<sCurrentLine.length() && sCurrentLine.charAt(i) != '>'){
if(sCurrentLine.charAt(i) == '#')
break;
s += sCurrentLine.charAt(i);
i++;
}
if(s=="")
break;
String s1 = s;
s = s.trim();
if(hm.containsKey(s)){
count = hm.get(s);
hm.put(s,count+1);
}
else{
hm.put(s,1);
}
s = s1+'>';
i++;
}
}
System.out.println(hm);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
class WordCount {
private static final String FILENAME = "/Users/MacOs/Desktop/taxonomy.en-US.txt";
public static void main(String[] args) {
BufferedReader br = null;
FileReader fr = null;
Map <String, Integer> hm= new HashMap<String, Integer>();
int count =0;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
int i =0;
String s = "";
while(i<sCurrentLine.length()){
while(i<sCurrentLine.length() && sCurrentLine.charAt(i) != '>'){
if(sCurrentLine.charAt(i) == '#')
break;
s += sCurrentLine.charAt(i);
i++;
}
if(s=="")
break;
String s1 = s;
s = s.trim();
if(hm.containsKey(s)){
count = hm.get(s);
hm.put(s,count+1);
}
else{
hm.put(s,1);
}
s = s1+'>';
i++;
}
}
System.out.println(hm);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
public class TaxonomyCount {
public static void main(String[] args) throws IOException {
Map<String, Integer> map = new ConcurrentHashMap<>();
Path filePath = Paths.get("taxonomy.txt");
List<String> lines = Files.lines(filePath).collect(Collectors.toList());
for (String line : lines) {
if (line.startsWith("#") || !line.startsWith("Electronics"))
continue;
StringBuffer sb = new StringBuffer();
String[] tokens = line.split(">");
for (int i = 0; i < tokens.length; ++i) {
sb.append(tokens[i].trim());
int count = map.getOrDefault(sb.toString(), 0);
map.put(sb.toString(), ++count);
if (i < tokens.length - 1) {
sb.append(">");
}
}
}
System.out.println(map.get("Electronics>Arcade Equipment"));
System.out.println(map.get("Electronics>Arcade Equipment>Skee-Ball Machines"));
}
}
import java.io.*;
- kiran khatal June 07, 2017import java.util.*;
public class Question_1 {
public static void main(String[] args) {
String Ifile="\\taxonomy.en-US.txt"; //input file
String Outputfile="\\Output.txt"; //output file
BufferedReader br;
FileReader fr;
File Ofile;
try
{
fr=new FileReader(Ifile);
br=new BufferedReader(fr);
Ofile=new java.io.File(Outputfile);
FileOutputStream fos=new FileOutputStream(Ofile);
PrintWriter pw=new PrintWriter(fos);
Set<String> hs=new HashSet<String>();
int StringLen=0;
String currentLine;
LinkedHashMap<String,Integer> map= new LinkedHashMap<String, Integer>();
while((currentLine= br.readLine()) != null )
{
currentLine += " >";
currentLine=currentLine.trim();
StringLen=currentLine.length();
while(StringLen>0)
{
int n=currentLine.indexOf(">", StringLen-1);
String sub=currentLine.substring(0, n);
hs.add(sub);
int ln=sub.lastIndexOf(">", n-2);
StringLen=ln;
}
for(String strcontain:hs)
{
if(map.containsKey(strcontain))
{
map.put(strcontain, map.get(strcontain)+1);
}
else
{
map.put(strcontain, 1);
}
}
hs.clear();
}
for (Map.Entry m:map.entrySet())
{
pw.println(m.getKey()+"="+m.getValue());
}
System.out.println("Output File Write Completed");
pw.flush();
pw.close();
fos.close();
br.close();
fr.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}