VMWare Inc Interview Question for MTSs


Country: United States
Interview Type: In-Person




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

public static void start()
        {
            string inputStr = "aaabb";
            Console.WriteLine(inputStr + " = " + cutString(inputStr));

            inputStr = "aaabbbb";
            Console.WriteLine(inputStr + " = " + cutString(inputStr));

            inputStr = "aaabbb";
            Console.WriteLine(inputStr + " = " + cutString(inputStr));

            inputStr = "aabbbbbb";
            Console.WriteLine(inputStr + " = " + cutString(inputStr));

            inputStr = "aabbbba";
            Console.WriteLine(inputStr + " = " + cutString(inputStr));
        }

        public static bool cutString(string inputStr)
        {
            Dictionary<char, int> charCount = countChars(inputStr);
            // TODO: Make sure the charCount.keys == 2

            char char1 = charCount.Keys.First();
            char char2 = charCount.Keys.Last();
            if (charCount[char1] == charCount[char2]) {return true; }

            // Figure out which one to remove, and how many
            char toRemove;
            int removeCount;
            if (charCount[char1] > charCount[char2])
            {
                toRemove = char1;
                removeCount = charCount[char1] - charCount[char2];
            }
            else
            {
                toRemove = char2;
                removeCount = charCount[char2] - charCount[char1];
            }

            // Start cutting from left
            int leftCount = findRepeat(inputStr, toRemove, true);
            if (leftCount >= removeCount) { return true; }

            // Try cutting from right, but substract first
            removeCount -= leftCount;

            int rightCount = findRepeat(inputStr, toRemove, false);
            if (rightCount >= removeCount) { return true; }

            return false;
        }

        private static int findRepeat(string inputStr, char toFind, bool leftToRight)
        {
            int numOfRepeat = 0;
            char[] inputChars = inputStr.ToCharArray();

            if (!leftToRight) { inputChars = inputChars.Reverse().ToArray(); }

            for (int i = 0; i < inputStr.Length; i++)
            {
                if (inputChars[i] != toFind) { break; }
                numOfRepeat++;
            }

            return numOfRepeat;
        }

        private static Dictionary<char, int> countChars(string inputStr)
        {
            Dictionary<char, int> charCount = new Dictionary<char, int>();
            foreach (char chr in inputStr)
            {
                int count = 0;
                if (charCount.ContainsKey(chr))
                {
                    count = charCount[chr];
                    charCount.Remove(chr);
                }
                charCount.Add(chr, ++count);
            }

            return charCount;
        }

- therearetwouws May 19, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def find_cut_point(some_string){
  counter = 0 
  a_counter_list = list( some_string.toCharArray ) as {
    // cumulative count of 'a' at index
    ( $.o == _'a' ) ? ( counter += 1 ) : counter  
  }
  N = size(some_string)
  A_count = a_counter_list[-1] 
  B_count = N - A_count 
  exists( a_counter_list ) where {
    // $.o is the current item
    // $.i is the index of the item 
    current_b_count = $.i + 1 - $.o 
    count_a_which_will_be_left = A_count - $.o 
    count_b_which_will_be_left = B_count - current_b_count 
    count_a_which_will_be_left == count_b_which_will_be_left
  }
}

find_cut_point( "aaabbb" )

- NoOne May 20, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

let dp[i][j]=frequency of jth character(in ascii) till the ith index.
example: "aabc"
dp[0]['a']=1

dp[2]['a']=2
dp[2]['b']=1

dp[3]['a']=2
dp[3]['b']=1
dp[3]['c']=1

N=length of String

int ans;
 for(int i=0;i<N;i++){
  ans=i;
  for(int j=1;j<255;j++){
	if(dp[i][j]!=dp[N-1][j]-dp[i][j]){
           ans=-1;
	   break;		
	}
      }
     if(ans!=-1)break;
    }
    return ans;

- pankaj.kumar.01729 May 23, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

///Also works for string like "aabacabab" and "aabacaabbccdd"

public static bool SameFrequencyCharacters(string input)
        {
            var charFrequency = new Dictionary<char, int>();

            for (int i = 0; i < input.Length; i++)
            {
                FillDictionary(charFrequency, input, i);
               
                var matchFound = ValidateData(charFrequency);

                if (matchFound)
                {                    
                    return true;
                }
            }

            var reverseCharFrequency = new Dictionary<char, int>();

            for (int i = input.Length - 1; i >= 0; i--)
            {
                FillDictionary(reverseCharFrequency, input, i);
               
                var matchFound = ValidateData(reverseCharFrequency);

                if (matchFound)
                {
                    return true;
                }
            }

            return false;
        }

        public static bool ValidateData(Dictionary<char, int> charFrequency)
        {
            if (charFrequency.Count < 2)
            {
                return false;
            }

            int prevCount = 0;
            foreach (var element in charFrequency)
            {
                prevCount = element.Value;
                break;
                
            }
            foreach (var element in charFrequency)
            {
                if (element.Value != prevCount)
                {
                    return false;
                }
            }

            return true;
        }

        public static void FillDictionary(Dictionary<char, int> frequencyData, string input, int counter)
        {
            if (frequencyData.ContainsKey(input[counter]))
            {
                frequencyData[input[counter]] = frequencyData[input[counter]] + 1;
            }
            else
            {
                frequencyData.Add(input[counter], 1);
            }
        }

- Manoj May 26, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean sameFrequencyCharactersInString(String str) {
HashMap<Character,Integer> map = new HashMap();

for(int i =0; i< str.length(); i++) {
map.put(str.charAt(i), map.getOrDefault(str.charAt(i), 0)+1);
}
Set<Integer> values = new HashSet<Integer>();
for (Integer value : map.values()) {
if (!values.add(value)) {
return true;
}
}
return false;
}

- anonymous June 01, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean sameFrequencyCharactersInString(String str) {
			  HashMap<Character,Integer> map = new HashMap();
			  
			  for(int i =0; i< str.length(); i++) {
				  map.put(str.charAt(i), map.getOrDefault(str.charAt(i), 0)+1);
			  }
			  Set<Integer> values = new HashSet<Integer>();
			  for (Integer value : map.values()) {
			    if (!values.add(value)) {
			      return true;
			    }
			  }
			  return false;
		  }

- anonymous June 01, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if the strings will be as per questions -> all the 'a's will be on left and all the 'b's on the right:

bool cutString(std::string str)
{
	if (str.length() <= 1 || (str.length()%2 ==1))
	{
		return false;
	}

	int countaleft = 0, countbright = 0;
	int length = int(str.length());
	for (int i = 0; i < length/ 2; ++i)
	{
		if (str[length - i - 1] == 'b')
			++countbright;
		else
			return false;

		if (str[i] == 'a')
			++countaleft;
		else
			return false
	}
	return (countaleft == countbright);
}

if string passed has jumbled 'a's and 'b's only then 2 more counts can be added countaright and countbleft

if string passed can have every alphabet then 2 maps of 26 counts each for every character and at the end just compare each count , one mismatch and return false.

- doonsMonk July 17, 2019 | 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