I presented a code solution to a startup as part of their interview polity.
The problem statement is to find the frequency of words occurring in a sentence. I got the code rejected saying, "Evaluating code is always subjective, but people generally leave a "fingerprint" with their design approach and the nature of their code (comments, tests, multiple solution paths, validation). The hiring manager has a high bar, and has rejected many working solutions because he didn't like the approach or the number of trees used or linear scaling vs. ^2 scaling."
I have pasted my solution. The hiring manager specified not to use collection API. I would like some opinions as to how I would have improved the code.
import java.io.*;
import java.util.*;
class FreqWord {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new
InputStreamReader(System. in ));
String str[] = new String[30];
StringTokenizer st;
int i = 0, j, k, ctr, size = 0;
System.out.println(" Enter the String :");
String str1 = br.readLine();
st = new StringTokenizer(str1, " \n");
while (st.hasMoreTokens()) {
str[i] = st.nextToken();
size++;
i++;
}
System.out.println("\n-------------");
System.out.println("\nWord" + "\t" + "freq");
System.out.println("\n-------------");
for (i = 0; i < size; i++) {
ctr = 0;
for (j = i; j < size; j++) {
if (str[j].equals(str[i])) ctr++;
}
for (k = i - 1; k >= 0; k--) {
if (str[k].equals(str[i])) {
ctr = 0;
break;
}
}
if (ctr != 0) {
System.out.println(str[i] + "\t" + ctr);
}
}
System.out.println("\n------------");
}
}
Multiset
could do this in a few lines of code. \$\endgroup\$ – azz Oct 31 '13 at 17:15if
,for
, orwhile
) and hiring manager limitation (not usingCollections
. Plus your code has bothif
andwhile
. So, what is the exact and complete task here? \$\endgroup\$ – PM 77-1 Oct 31 '13 at 17:29split()
method can simplify the code a bit. \$\endgroup\$ – PM 77-1 Oct 31 '13 at 17:33