Adobe Interview Question
Software Engineer / DevelopersTo prepare a heap of n node it takes O(n) time(with comparator on frequency)
To find the k most occuring strings.Just remove the strings from heap k times.
Every deletion takes log(n) time.So it is n+klogn.
If k i constant then its O(n)...:)
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char a1[]={'a','b','c','b','c','d','d','b'};
int b[128];
for(int k=0;k<128;k++)
b[k]=0;
for(int i=0;i<strlen(a1);i++)
b[(int)a1[i]]++;
for(i=0;i<128;i++)
{
if(b[i]>0)
cout<<"\n"<<(char)i<<" repeats\t"<<b[i]<<"times";
}
getch();
}
wat about "strings"?? a buffer of 128 size wont work in that case..
use hashmap
where key=string and value =count of occurances
moreover question asks 5 most occuring string, how does this solution takes care of that
I think using java classes like treemap or hashmap would not be accepted.
Anyways i too had thought of a hash table sort of structure.
We can design our own hashtable class but ultimately need to have a way to sort the hashtable. In that case it will take O(n) for scanning the elements and putting their counts in hashtable+ O(mlogm) for sorting the hashtable elements. I may b wrng but mlogm could be taken as constant as it depends on number of possible elements as keys
I think using java classes like treemap or hashmap would not be accepted.
Anyways i too had thought of a hash table sort of structure.
We can design our own hashtable class but ultimately need to have a way to sort the hashtable. In that case it will take O(n) for scanning the elements and putting their counts in hashtable+ O(mlogm) for sorting the hashtable elements. I may b wrng but mlogm could be taken as constant as it depends on number of possible elements as keys
I think using java classes like treemap or hashmap would not be accepted.
Anyways i too had thought of a hash table sort of structure.
We can design our own hashtable class but ultimately need to have a way to sort the hashtable. In that case it will take O(n) for scanning the elements and putting their counts in hashtable+ O(mlogm) for sorting the hashtable elements. I may b wrng but mlogm could be taken as constant as it depends on number of possible elements as keys
I think using java classes like treemap or hashmap would not be accepted.
Anyways i too had thought of a hash table sort of structure.
We can design our own hashtable class but ultimately need to have a way to sort the hashtable. In that case it will take O(n) for scanning the elements and putting their counts in hashtable+ O(mlogm) for sorting the hashtable elements. I may b wrng but mlogm could be taken as constant as it depends on number of possible elements as keys
I would create two data structures. 1 Hash map, 1 hashset. The hashmap will have key as character and value as count. hashset will work as a placeholder for the items that are already added to hashmap. So
if (!hashset.add("a")) {
i=hashmap.get("a");
i++;hashmap.put("a",i)
}
else
hashmap.put("a",i)
Iterate thru the whole array and calculate the frequency.
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class CommonOccuring {
public static void main(String[] args) {
String[] arr = { "a", "b", "c", "f", "a", "d", "e", "f", "b", "f", "f" };
Map<String, Integer> map = new HashMap<String, Integer>();
int count = 1;
map.put(arr[0], count);
for (int i = 1; i < arr.length; i++) {
if (map.containsKey(arr[i])) {
map.put(arr[i], map.get(arr[i])+1);
}else{
map.put(arr[i], 1);
}
}
Iterator<Map.Entry<String, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, Integer> entry = entries.next();
System.out.println( entry.getKey() + " " + entry.getValue());
}
}
}
public Hashtable countAlphabet (String astring) {
Hashtable table = new Hashtable(); If (astring.length> 4000) return table;
StringBuffer buffer = new StringBuffer(aString);
while (buffer.length() > 0){ String firstChar = buffer.substring(0, 1);
Integer count (Integer) table.get(firstChar);
if (count == null){ count = new Integer (1);
}else{
}
Count = new Integer (count. intvalue() + 1);
table.put(firstChar, count); buffer.delete(0, 1);
} return table;
public Hashtable countAlphabet (String astring) {
Hashtable table = new Hashtable(); If (astring.length> 4000) return table;
StringBuffer buffer = new StringBuffer(aString);
while (buffer.length() > 0){ String firstChar = buffer.substring(0, 1);
Integer count (Integer) table.get(firstChar);
if (count == null){ count = new Integer (1);
}else{
}
Count = new Integer (count. intvalue() + 1);
table.put(firstChar, count); buffer.delete(0, 1);
} return table;
Create a max heap taking freq as the comparison point
- ibnipun10 August 04, 2010Now the first 5 elements will be the 5 most common
Time complexity O(nlogn)