Google Interview Question for Software Engineers


Country: United States




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

package google;

import java.util.NavigableSet;
import java.util.Scanner;
import java.util.TreeSet;

public class LexicographicalNextWord {
	public static void main(String args[]) {
		NavigableSet<String> words = new TreeSet<String>();
		Scanner input = new Scanner(System.in);
		System.out.println("√čnter number of words: ");
		int numberOfWords = input.nextInt();
		for (int i = 0; i < numberOfWords; i++) {
			words.add(input.next());
		}

		System.out.println("√čnter input word: ");
		String word = input.next();

		String out = null;
		if (!words.contains(word)) {
			out = words.ceiling(word);
		} else {
			out = words.higher(word);
		}
		System.out.println(out == null ? "Error" : out);
	}
}

- Dhruva.Bharadwaj April 16, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using trie to solve this problem:

1. build a 26-ary trie
2. doing a inorder traversal of this trie
3. find the (closest) next word

- respectmyauthoritah April 16, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class FindNextLexogWord {


	public static void main(String[] args) {

		String[] wordList = {"Cat", "dog", "cow", "donkey", "zebra", "monkey"};
		String input = "duck";
		int nextLex = Integer.MIN_VALUE;
		int value = 0;
		String word = null;
		for( int i = 0 ; i < wordList.length ; i ++ ) {
			if(( value =  input.compareTo(wordList[i]))  <  0 ) {
				System.out.println(value);
				if( nextLex < value){ 
				nextLex = value;
				word = wordList[i];
				}
			}
		}


	}

- arcpill April 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String findNextWord(String[] words, String input) {
		// go through each word
		int closest = 0;// assume closest itself
		String res = input;
		for(String word: words) {
			// compare input with the word
			// since we are looking for more closer word lets look for < 0
			if(input.compareTo(word) < 0) {
				// which means input comes first
				// check if there is any existing one?
				if(closest == 0) {
					// first time we found a closet
					res = word;
					closest = input.compareTo(word);
				} else {
					// if there is exisrts
					if(closest < input.compareTo(word)) {
						res = word;
						closest = input.compareTo(word);
					}
				}
			}
		}
		
		return res;
	}

- Rj April 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use TST( Ternary Search Tree):
Try using display method coz it generally uses inorder traversal. if your searched element is found, the next element in the inorder traversal would be your next lexigraphic value.

I have solved this question. Please mail me if you need explanation.

class TSTNode{
Character data;
TSTNode left;
TSTNode right;
TSTNode middle;
boolean isWord;

public TSTNode(Character data){
this.data=data;
}

}

- prahl May 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using tree.

#include <string>
#include <memory>
#include <iostream>

class Node {
 public:
  Node(const std::string &word) : word_(word) {}
  const std::string &word() const { return word_; }

  static void insertWord(std::unique_ptr<Node> &tree, const std::string &word)
  {
    if (tree.get() == nullptr)
      tree.reset(new Node(word));
    else if (word < tree->word())
      insertWord(tree->childs_[0], word);
    else
      insertWord(tree->childs_[1], word);
  }

  std::string findNextWord(const std::string &word)
  {
    if (word < word_) {
      std::string w;
      if (childs_[0])
        w = childs_[0]->findNextWord(word);
      return w.empty() ? word_ : w; // if less than me not found then return me
    } else {
      if (!childs_[1])
        return std::string(); // not found
      return childs_[1]->findNextWord(word);
    }
  }

private:
  std::string word_;
  std::unique_ptr<Node> childs_[2];
};

int main() {
  std::unique_ptr<Node> tree;
  for (decltype(auto) w : {"cat", "dog", "cow", "donkey", "zebra", "monkey"})
    Node::insertWord(tree, w);

  std::string w;
  while (true)
  {
    std::cout << "Input: ";
    std::cin >> w;
    if (std::cin.eof())
      break;
    std::cout << "Output: " << tree->findNextWord(w) << std::endl;
  }
}

- S.M. January 07, 2018 | 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