Amazon Interview Question
Country: United States
Is it the correct way as expected.
package com.amazon.universe.main;
import java.io.File;
import java.util.LinkedList;
import java.util.Queue;
public class FindFileswithGivenExtension {
public static void main(String[] args) {
/* define the appropriate file path here*/
File file = new File("D:\\G_Mlab\\neural_network"), temp;
Queue<File> allFiles = new LinkedList<File>();
allFiles.add(file);
File[] files = null;
while (!allFiles.isEmpty()) {
temp = allFiles.poll();
files = temp.listFiles();
for (File innerFile : files) {
if (innerFile.isDirectory())
allFiles.add(innerFile);
else if (innerFile.isFile()
&& innerFile.getName().endsWith(".pdf")) {
System.out.println(innerFile.getAbsolutePath());
}
}
}
System.out.println(files.length);
}
}
The interviewer is probably interested if you can use bash instead of coding something already implemented very well If this is the case than I would reply something like:
find temp/reg -name '*.c'
Or it might just be the opposite. He may be interested in testing if you know syscalls and how find works under the hood.
Is it the right way?? please suggest. :)
=====================================
- Gautam Tyagi July 19, 2015