Interview Question


Country: United States
Interview Type: Written Test




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

int dups = 0;
        String sentence = ...; // Some value here
        Set<String> set = new HashSet<String>();
        for (String w : sentence.split(" ")) {
            if (!set.add(w)) dups++;
        }

        System.out.println(dups);

- avl July 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a trie. Read words from the string and add them to the trie if not present previously, else increment the count.

Traverse the trie and print out the words that have count > 1

A hashtable, with word as key and count as value should also work just fine.

- Murali Mohan July 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if you dont have a constraint on the memory usage, then use a hash map and store each unique word as key and its count as value.

- Anonymous July 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Cant we use if (!set.contains(w)) to check if a word already exists?

- Anjali July 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If there is a constraint on space, sort the array and count the consecutive blocks of values. Output the block that is longest. Here is the pseudo code:

SortArray();  <-- O(nlgn)
total=0;
for(i=0;i<n;i++)
{
cnt=1; //there is at least one element i.e., a[i] itself
if(a[i]==a[i+1])
cnt++;
else
{
total+=cnt;
printf("%d is repeated %d times",a[i],cnt);
cnt=0;
}
}

- Anonymous July 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes we can use the classes which implement set in java its easy , bit alternative is using the java collections.sort .
1) First split the string and put that to ArrayList
2) sort the arraylist using the collections sort
3) write a business logic to find the duplicates using list[i] == list [i+1]
#################################################################

package com.CareerCup;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class DuplicateString implements Comparator<String> {
public static void main(String s[])
{
String str = "this is string of string find is this string ";
String[] splittedString= str.split(" ");
ArrayList<String> stringList = new ArrayList<String>();
ArrayList<String> duplicateList = new ArrayList<String>();

for(String string : splittedString)
{
stringList.add(string);
}
Collections.sort(stringList, new DuplicateString());

int listLen= stringList.size();

for(int i=0;i<listLen-1;i++)
{
if(stringList.get(i).equals(stringList.get(i+1)))
{
duplicateList.add(stringList.get(i));
}
}

System.out.println( duplicateList);


}

@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
}

- Mahanth Hiremath ( mahanthopensource@gamil.com) August 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

HashMap is a better choice, with key as word and count as the value.
Traverse through the list of words and add all of them to the hashmap and print all.

- Niraj September 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;


public class FindStringCount {

Hashtable resultHash = new Hashtable();
public static void main(String[] args) {
// TODO Auto-generated method stub
int dups = 0;
String sentence = "string this is a string for a string"; // Some value here
Set<String> set = new HashSet<String>();
for (String w : sentence.split("\\s+")) {
//System.out.println(w);
if (!set.add(w)) dups++;
}

System.out.println(set);
FindStringCount searchCount = new FindStringCount();
Iterator ite = set.iterator();

while(ite.hasNext()){
searchCount.process(sentence, ite.next());
}
System.out.println(searchCount.resultHash);
//searchCount.process(sentence, searchChar);

}

private void process(String sentence, Object searchChar){
int count = 0;
for (String w : sentence.split("\\s+")) {
if(searchChar.equals(w)) count++;
}

if(count >0 ) {
resultHash.put(searchChar, count);
}


}



}

- Venkatesh Kumar D August 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

simple array of integer of size 256

- deb July 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How can we find word from a string.Sounds little confusing. Is it sentence? Please help anyone

- deb July 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how is it possible with array of size 256. We are talking about possible words..not the letters

- amd July 19, 2013 | Flag


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