Google Interview Question for Senior Software Development Engineers


Team: TEZ
Country: India
Interview Type: Phone Interview




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

#include <iostream>
#include <array>
#include <string>
#include <algorithm>

using std::array;
using std::string;

void ResetArray(array<int, 6> &a) {
    std::fill(a.begin(), a.end(), -1);
}

void ResetArrayBelowNum(array<int, 6> &a, int t) {
  for (auto &n : a) {
    if ((n) < t) {
      n = -1;
    }
  }
}

int ReportIndexPerm(const string &s) {
  if (s.size() == 0) {
    return -1;
  }

  array<int, 6> letters;
  ResetArray(letters);
  int resultIndex = -1;
  int numMatchesFound = 0;
  int A = 'A';
  for (size_t i = 0; i < s.size(); ++i) {
    int index = s[i] - A;
    if (index > -1 && index < letters.size()) {
      if (letters[index] == -1) {
        if (resultIndex == -1) {
          resultIndex = i;
        }
        letters[index] = i;
        numMatchesFound++;
      }
      else if (resultIndex != -1) {
        numMatchesFound -= (letters[index] - resultIndex);
        resultIndex = letters[index] + 1;
        ResetArrayBelowNum(letters, letters[index]);
        letters[index] = i;
      }
    }
    else {
      resultIndex = -1;
      numMatchesFound = 0;
      ResetArray(letters);
    }

    if (numMatchesFound == letters.size()) {
      return resultIndex;
    }
  }

  return -1;
}

int main(int argc, char **argv) {

  string s{argv[1]};
  std::cout << s << "\n";

  auto res = ReportIndexPerm(s);
  std::cout << res << "\n";
  if (res != -1) {
    std::cout << s[res] << "\n";
  }

  return 0;
}

- thelobster February 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class DetectStringPermutations 
{
	public int getPermutationIndex(String input)
	{
		int index = -1;
		if(input != null && input.length() >= 6)
		{
			String s = input.toUpperCase();
			int N = input.length();
			int i, validSegmentSize = 0, j, lastSeenIndex;
			int[] indices = new int[6]; // to keep track of indices of A,B,C,D,E,F
			
			Arrays.fill(indices, -1);
			
			i = 0;
			char currentCharacter;
			
			while(i < N)
			{
				currentCharacter = s.charAt(i);
				if(currentCharacter >= 'A' && currentCharacter <= 'F')
				{
					// valid character found
					
					// Case 1 : It is a new character in the fragment seen till now
					if(indices[currentCharacter -'A'] == -1)
					{
						indices[currentCharacter - 'A'] = i;
						++validSegmentSize;
						
						// if permutation found - break !
						if(validSegmentSize == 6)
						{
							// this i is the 6th character of the fragment
							index = (i - 5);
							break;
						}
					}
					else
					{
						// duplicate character !!
						lastSeenIndex = indices[currentCharacter - 'A'];
						
						// All characters seen at existing index and before that stand invalid !
						
						// e.g. if we see EADCBA, then as we have seen a duplicate A - the EA 
						// will never ever be a part of valid permutation. Start looking at 
						// DCBA onwards
						
						for(j = 0; j < 6; j++)
						{
							if(indices[j] <= lastSeenIndex && indices[j] >= 0)
							{
								indices[j] = -1;
								--validSegmentSize;  // reduce fragment size accordingly
							}
						}
						
						++validSegmentSize; // Do consider the currently seen duplicate valid character
					}
				}
				else
				{
					// reset the fragment seen till now if there is a violation
					if(validSegmentSize > 0)
					{
						Arrays.fill(indices, -1);
					}
					
					validSegmentSize = 0;
				}
				
				++i;
			}
		}
		
		return index;
	}
}

- Interviewer February 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using simple integer.
int i = 0;
1. For each character in set S = {A, B, C, D, E, F}. Assign the following values:
A = 1, B = 10 = 2, C = 100 = 4, D = 1000 = 8, E = 10000 = 16, F = 100000 = 32.
2. Start from the first character. If the character is not in S ( if 0 <= (c - 'A') < 6 ) then set i to 0. Otherwise if the & of assigned character value ( 1 << (c - 'A') ) with i is not zero then go backward until you reach to the same character and start from the character right after that. If neither of these two cases are correct set the value of i to or ( | ) of its value and the assigned value of the character. Once the value of i reaches to 63 we have found a permutation.

- nooreddin February 14, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use a HashSet.
Put all characters of ABCDEF (lookup string) in hashset.
Parse your source string and check of if exists in hashset . If found (remove that element from this hashset - and copy it over to another one , so we dont have duplicate lookups), keep moving the index on source string and check if hashset matches. After moving the index by length of lookup string, the hashset should be empty. return currentIndex - length. If its not empty, discard the hashset and create a fresh one to start looking for currentIndex - length +1 .

- maddy February 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

VALID_CHARS = set('ABCDEF')

def find_first_permutation(string):
    FOUND_VALID_CHAR = {}

    start_index = 0

    for end_index in range(len(string)):
        end_char = string[end_index]

        if end_char not in VALID_CHARS:
            FOUND_VALID_CHAR = {}
            start_index = end_index + 1

        elif end_char not in FOUND_VALID_CHAR:
            FOUND_VALID_CHAR[end_char] = None
            if len(FOUND_VALID_CHAR) == len(VALID_CHARS):
                # print string[start_index:end_index+1]
                return start_index

        else:
            while string[start_index] != end_char:
                del FOUND_VALID_CHAR[string[start_index]]
                start_index += 1
            start_index += 1

    return -1


if __name__ == '__main__':
    print find_first_permutation('ZZZBCDEAFZZZ')
    print find_first_permutation('ABCCDABCFFABCDEE')

- saishg March 14, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
        ValidPermutationString validPermutationString = new ValidPermutationString();

        System.out.println(validPermutationString.indexFindPermutation("BCDEFABCDGH", "ABCD"));
    }

    int indexFindPermutation(String input, String pattern) {
        HashMap<Character, Integer> map = new HashMap<>();
        for (char ch: pattern.toCharArray()) {
            map.put(ch, map.getOrDefault(ch,0 )+1);
        }

        for (int i=0; i < input.length() && i < pattern.length(); i++) {
            char ch = input.charAt(i);
            Integer integer = map.getOrDefault(ch, 0);
            if (integer == 1) {
                map.remove(ch);
            } else {
                map.put(ch, integer -1);
            }
        }
        if (map.size() == 0) return 0;

        int size = pattern.length();
        for (int i = pattern.length(); i < input.length(); i++) {
            char oldChar = input.charAt(i - size);
            Integer orDefault = map.getOrDefault(oldChar, 0);
            // add this
            if (orDefault == -1) map.remove(oldChar);
            else map.put(oldChar, orDefault + 1);

            // subtract this
            char currentChar = input.charAt(i);
            Integer currentSize = map.getOrDefault(currentChar, 0);
            if (currentSize == 1) map.remove(currentChar);
            else map.put(currentChar, currentSize -1);

            if (map.size() == 0)
                return i - size + 1;
        }

        return -1;
    }

- Cooked March 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int findIndex(String str) {
        if (str == null || str.length() < 6) return -1;
        int hash = 0;
        str = str.toUpperCase();
        int index = -1;
        int len = str.length();
        int i = 0;
        for (i = 0; i < 6; i++) {
            char ch = str.charAt(i);
            int pos = ch - 'A';
            hash += (1 << pos);
        }
        if (hash == 63) return 0;
        for (i = 6; i < len; i++) {
            char ch = str.charAt(i);
            int pos = ch - 'A';
            hash += (1 << pos);
            ch = str.charAt(i-6);
            pos = ch - 'A';
            hash -= (1 << pos);
            if (hash == 63) return i-5;
        }
        return index;
    }

- Aim_Google March 31, 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