Apkudo Interview Question
SDE1sCountry: United States
Interview Type: Written Test
Java implementation.... comments are welcomed.
public void findAndShow(String s) {
Map<String,List<Integer>> map = new HashMap<String, List<Integer>>();
String lines[] = s.split("\n");
for(int i=0; i < lines.length; i++) {
String words[] = lines[i].split("\\s+");
if(words.length == 0)
continue;
for(int j=0; j < words.length; j++) {
String word = words[j].toLowerCase();
List<Integer> wordLines = map.get(word);
if(wordLines == null)
wordLines = new LinkedList<Integer>();
wordLines.add(i+1);
map.put(word, wordLines);
}
}
String words[] = map.keySet().toArray(new String[0]);
List<String> keys = Arrays.asList(words);
Collections.sort(keys);
for(String word: keys) {
System.out.print(word+" ");
for(Integer line: map.get(word)) {
System.out.print(line+" ");
}
System.out.println("");
}
}
This is what should be done ( ZoomBA )
d = fold ( file( 'foo.txt') , sdict() ) -> {
line_no = $.i + 1
partial = $.p
line = $.o
tokens ( line , '\S+') -> {
word = $.o.toLowerCase
if ( word @ partial ){
partial[word] += line_no
} else {
partial[word] = list( line_no )
}
}
$.p // return the partial
}
// print the stuff
for ( d ) { printf('%s : %s\n', $.key , str( $.value , ' ' ) ) }
C# implementation:
string str = "This is\nsome kind OF text it\nIs an example of text";
List<Store> store = new List<Store>();
List<string> storing = new List<string>();
string[] newArr = str.Split('\n');
for (int i = 0; i < newArr.Length; i++)
{
var massive = newArr[i].ToString().ToLower().Split(new char[0],
StringSplitOptions.RemoveEmptyEntries);
store.Add(new Store(massive , i + 1));
}
foreach (var item in store)
{
for (int i = 0; i < item.array.Count(); i++)
{
Console.WriteLine("{0} : {1}",item.array[i], item.number);
}
}
class Store
{
public int number;
public string[] array;
public Store(string[] array, int number)
{
this.number = number;
this.array = array;
}
}
package algorithms.sorting;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class P3 {
public static void main(String[] args) throws IOException {
Map<String, List<Integer>> map = new HashMap<>();
final int[] count = {0};
try (Stream<String> stream = Files.lines(Paths.get("test.txt"))) {
stream.forEach(line -> {
count[0] = count[0] + 1;
List<String> words = Arrays.stream(line.split(" ")).map(String::toLowerCase).collect(Collectors.toList());
words.forEach(w -> {
if(!map.containsKey(w)) {
map.put(w, new ArrayList<>());
}
map.get(w).add(count[0]);
});
});
}
map.forEach((key, value) -> {
System.out.print(key + " ");
value.forEach(v -> {
System.out.print(v + " ");
});
System.out.println();
});
}
}
Id did it with a min heap. I don't know how efficient it was but worked and they liked it!
- yasharonline January 11, 2017I created a 'Word' class which store 'string word' and 'List<int> line_numbers'.
Please let me know your ideas and make sure to pay attention to the constraint 'Large Text File'.