Coding exercise:
Find the first word in a stream in which it is not repeated in the rest of the stream. Please note that you are being provided a stream as a source for the characters. The stream is guaranteed to eventually terminate (i.e. return false from a call to the hasNext() method), though it could be very long. You will access this stream through the provided interface methods. A call to hasNext() will return whether the stream contains any more characters to process. A call to getNext() will return the next character to be processed in the stream. It is not possible to restart the stream.
Example:
Input: The angry dog was red. And the cat was also angry.
Output: dog
In this example, the word ‘dog’ is the first word in the stream in which it is not repeated in the stream.
Use one of the following skeletons for your solution:
Java:
package questions;
public interface Stream {
char getNext();
boolean hasNext();
}
public class QuestionA {
public static String firstWord(final Stream input) {
return "";
}
}C++
class Stream {
public:
char getNext();
bool hasNext();
};
class QuestionA {
std::string firstWord(Stream &input) {
return "";
}
};