Amazon Interview Question for Java Developers


Country: United States
Interview Type: Written Test




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

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);
}

}


}

- kiran khatal June 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Suffix trees.

- Anonymous May 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}

}


}

- KIran V Khatal June 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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();

			}

		}

	}

}

- Nidhi June 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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();

}

}

}

}

- Anonymous June 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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"));
	}
}

- learnsomething247 June 21, 2017 | Flag Reply


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