Apkudo Interview Question for SDE1s


Country: United States
Interview Type: Written Test




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

Id did it with a min heap. I don't know how efficient it was but worked and they liked it!
I 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'.

- yasharonline January 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

myfile=open('testfile','r')
wdict=dict()
myfile.seek(0)
linenum=1
for line in myfile:
   words=line.lower().split()
   for i in words:
     try:
        if len(wdict[i]) > 0:
          wdict[i].append(linenum)
     except:
        wdict[i]=[linenum]
   linenum += 1

for k,v in wdict.items():
   print k, v
myfile.close()

- Phu Sam January 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi. Did you take into account the case when say the heap size will overflow? Say, with the ram size of 1-2 GB there can be issues...?

- Nik January 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

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

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 , ' ' ) ) }

- NoOne January 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- west12as January 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think in last for loop
we can write this to meet the requirement.

for k,v in wdict.items():
vv = str(v).replace('[','').replace(',','').replace(']','')
print (k, vv)
myfile.close()

- rksahoo077 March 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- vaibhavsinh December 05, 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