Akamai Interview Question for Software Developers


Country: United States




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

/**
 * Created by M on 7/10/17.
 */
import java.util.*;
public class VowelSwitcher {
    public static void main (String args[]){
        Scanner in = new Scanner(System.in);

        System.out.print("Enter the word you wish to change : ");
        String word = in.next();



        char [] splitWord = word.toCharArray();
        boolean firstFound = false;
        boolean secondFound = false;
        int firstIndex=0,secondIndex=0;
        HashSet<Character> vowels = new HashSet<Character>();
        vowels.add('a');
        vowels.add('A');
        vowels.add('e');
        vowels.add('E');
        vowels.add('i');
        vowels.add('I');
        vowels.add('o');
        vowels.add('O');
        vowels.add('u');
        vowels.add('U');


        for (int i = 0; i < splitWord.length; i++ ){

            if (vowels.contains(splitWord[i]) && firstFound && !secondFound){
                secondFound = true;
                secondIndex = i;
                char temp = splitWord[firstIndex];
                splitWord[firstIndex] = splitWord[secondIndex];
                splitWord[secondIndex] = temp;
                firstFound = false;
                secondFound = false;
                i++;
            }

            if (vowels.contains(splitWord[i]) && !firstFound){
                firstFound = true;
                firstIndex = i;

            }
        }


        System.out.println(splitWord);
    }
}

- Anonymous July 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

My Java solution, complexity O(n), space O(n).

public String interchange(String s) {
    String vowels = "aeiouy";

    // array counter for count amount of each vowels in s
    int[] counter = new int[300];
    for (char c : s.toCharArray()) {
        if (vowels.contains(c + "")) {
            counter[c]++;
        }
    }

    // build answer
    StringBuilder builder = new StringBuilder();
    for (char c : s.toCharArray()) {
        // if c is vowels, search remain vowels in
        // counter from 'a' to 'z' and append to answer
        if (vowels.contains(c + "")) {
            for (char x = 'a'; x <= 'z'; x++) {
                // counter[x] > 0 mean vowels x is available
                if (counter[x] > 0) {
                    builder.append(x);
                    counter[x]--;
                    break;
                }
            }
        } else { // append other character to answer
            builder.append(c);
        }
    }

    return builder.toString();
}

- Anonymous July 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

My Java solution, complexity O(n), space O(n).

public String interchange(String s) {
    String vowels = "aeiouy";

    // array counter for count amount of each vowels in s
    int[] counter = new int[300];
    for (char c : s.toCharArray()) {
        if (vowels.contains(c + "")) {
            counter[c]++;
        }
    }

    // build answer
    StringBuilder builder = new StringBuilder();
    for (char c : s.toCharArray()) {
        // if c is vowels, search remain vowels in
        // counter from 'a' to 'z' and append to answer
        if (vowels.contains(c + "")) {
            for (char x = 'a'; x <= 'z'; x++) {
                // counter[x] > 0 mean vowels x is available
                if (counter[x] > 0) {
                    builder.append(x);
                    counter[x]--;
                    break;
                }
            }
        } else { // append other character to answer
            builder.append(c);
        }
    }

    return builder.toString();
}

- techinterviewquestion.com July 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

/**
 * Created by M on 7/10/17.
 */
import java.util.*;
public class VowelSwitcher {
    public static void main (String args[]){
        Scanner in = new Scanner(System.in);

        System.out.print("Enter the word you wish to change : ");
        String word = in.next();



        char [] splitWord = word.toCharArray();
        boolean firstFound = false;
        boolean secondFound = false;
        int firstIndex=0,secondIndex=0;
        HashSet<Character> vowels = new HashSet<Character>();
        vowels.add('a');
        vowels.add('A');
        vowels.add('e');
        vowels.add('E');
        vowels.add('i');
        vowels.add('I');
        vowels.add('o');
        vowels.add('O');
        vowels.add('u');
        vowels.add('U');


        for (int i = 0; i < splitWord.length; i++ ){

            if (vowels.contains(splitWord[i]) && firstFound && !secondFound){
                secondFound = true;
                secondIndex = i;
                char temp = splitWord[firstIndex];
                splitWord[firstIndex] = splitWord[secondIndex];
                splitWord[secondIndex] = temp;
                firstFound = false;
                secondFound = false;
                i++;
            }

            if (vowels.contains(splitWord[i]) && !firstFound){
                firstFound = true;
                firstIndex = i;

            }
        }


        System.out.println(splitWord);
    }
}

- anbessa July 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public String interchange(String s) {
    String vowels = "aeiouy";

    // array counter for count amount of each vowels in s
    int[] counter = new int[300];
    for (char c : s.toCharArray()) {
        if (vowels.contains(c + "")) {
            counter[c]++;
        }
    }

    // build answer
    StringBuilder builder = new StringBuilder();
    for (char c : s.toCharArray()) {
        // if c is vowels, search remain vowels in
        // counter from 'a' to 'z' and append to answer
        if (vowels.contains(c + "")) {
            for (char x = 'a'; x <= 'z'; x++) {
                // counter[x] > 0 mean vowels x is available
                if (counter[x] > 0) {
                    builder.append(x);
                    counter[x]--;
                    break;
                }
            }
        } else { // append other character to answer
            builder.append(c);
        }
    }

    return builder.toString();
}

-----
This code is not working for the below test cases:
Input: snahe
Output: snahe
Expected: sneha

Input: bashun
Output: bashun
Expected: bushan

if the vowels in order, then it will not interchange.
In our first input sneha, we expecting a should interchange with e and vice versa

- Bushan SC July 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public String interchange(String s) {
    String vowels = "aeiouy";

    // array counter for count amount of each vowels in s
    int[] counter = new int[300];
    for (char c : s.toCharArray()) {
        if (vowels.contains(c + "")) {
            counter[c]++;
        }
    }

    // build answer
    StringBuilder builder = new StringBuilder();
    for (char c : s.toCharArray()) {
        // if c is vowels, search remain vowels in
        // counter from 'a' to 'z' and append to answer
        if (vowels.contains(c + "")) {
            for (char x = 'a'; x <= 'z'; x++) {
                // counter[x] > 0 mean vowels x is available
                if (counter[x] > 0) {
                    builder.append(x);
                    counter[x]--;
                    break;
                }
            }
        } else { // append other character to answer
            builder.append(c);
        }
    }

    return builder.toString();
}

/*
This program is not working for the below test cases:
Input : snahe
Output : snahe
Expected Output : sneha

Input : bashun
Output : bashun
Expected Output : bushan

If the vowels is in order in a string, then interchange will not happen.
In our first test case, we are expecting a should interchange with e.
*/

- Bushan SC July 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are cleaner solutions. Suitably change it into Java.

def reverse_vowels_declarative(word){
  vowels = set( 'aeiou'.value ) // get into a set 
  vowels_in_word = select( word.value ) where {  $.o @ vowels }
  // put back 
  fold ( word.value , -1 ) as { 
    continue( !($.o @ vowels) )
    word.value[$.i] = vowels_in_word[$.p]
    $.p -= 1 
  }
word 
}

def reverse_vowels_imperative(word){
  vowels = set( 'aeiou'.value ) // get into a set 
  lp = 0 ; rp = size(word) - 1 
  while ( true ){
      while ( lp < rp && !(word[lp] @ vowels) ){ lp += 1 }
      while ( lp < rp && !(word[rp] @ vowels) ){ rp -= 1 }
      break(lp >= rp )
      t = word[rp]
      word.value[rp] = word[lp] 
      word.value[lp] = t   
      lp +=1 ; rp -= 1
  }  
  word // done 
}

println( reverse_vowels_imperative('vowels') )

- NoOne July 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashSet;
import java.util.Set;

//@author: Deepak Kumar Modi (from Wibmo Software)
public class ExchangeVowel {
public static void main(String[] args) {
String x = "vowels";
//String x = "sneha";
//String x = "continuous";
StringBuilder y = new StringBuilder(x);
Set<Character> set = new HashSet<Character>();
set.add('a');
set.add('e');
set.add('i');
set.add('o');
set.add('u');

String result="";
Character temp1=' ';
Character temp2=' ';
int index=0;

for(int i=0;i<x.length();i++){
if(set.contains(x.charAt(i))){
if(index>0) {
y.setCharAt(index, x.charAt(i));
}
if(temp1!=' ')
temp2 = temp1;

temp1 = x.charAt(i);
index = i;
y.setCharAt(index, temp2);
}
}
System.out.println(y.toString());
}
}

- Deepak kumar modi (Wibmo Software) October 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashSet;
import java.util.Set;

//@author: Deepak Kumar Modi (from Wibmo Software)
public class ExchangeVowel {
	public static void main(String[] args) {
		String x = "vowels";
		//String x = "sneha";
		//String x = "continuous";
		StringBuilder y = new StringBuilder(x);
		Set<Character> set = new HashSet<Character>();
		set.add('a');
		set.add('e');
		set.add('i');
		set.add('o');
		set.add('u');

		String result="";
		Character temp1=' ';
		Character temp2=' ';
		int index=0;
		
		for(int i=0;i<x.length();i++){			
			if(set.contains(x.charAt(i))){				
				if(index>0) {
					y.setCharAt(index, x.charAt(i));					
				}
				if(temp1!=' ')
					temp2 = temp1;
				
				temp1 = x.charAt(i);
				index = i; 			
				y.setCharAt(index, temp2);
			}
		}
		System.out.println(y.toString());
	}
}

- Deepak kumar modi (Wibmo Software) October 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Unfortunately 3 times the code is pasted. Tried to remove duplicates but not successful. Sorry guys.

- deepakmodi2006 October 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class VowelsInterchange {

	public void Vowels(String str) {
		List<Character> vowels = new ArrayList<Character>();
		vowels.add('a');
		vowels.add('e');
		vowels.add('i');
		vowels.add('o');
		vowels.add('u');
		HashMap<Integer, Integer> hm = new HashMap<>();
		Integer fIndex = -1;
		for (int i = 0; i < str.length(); i++) {
			if (vowels.contains(str.charAt(i))) {
				if (fIndex != -1) {
					hm.put(fIndex, i);
					fIndex = -1;
				} else {
					fIndex = i;
				}
			}
		}
		SwapCharacters(str, hm);
	}

	public void SwapCharacters(String str, HashMap<Integer, Integer> hm) {
		String fSub = "";
		String sSub = "";
		String tSub = "";
		char fChar = ' ';
		char sChar = ' ';
		int fIndex = 0;
		int sIndex = 0;
		for (Map.Entry<Integer, Integer> entry : hm.entrySet()) {
			fIndex = entry.getKey();
			sIndex = entry.getValue();
			if (fIndex > 0)
				fSub = str.substring(0, fIndex);

			if (sIndex - fIndex > 1)
				sSub = str.substring(fIndex + 1, sIndex);

			if (sIndex < (str.length() - 1))
				tSub = str.substring(sIndex + 1, str.length());

			fChar = str.charAt(fIndex);
			sChar = str.charAt(sIndex);

			
			str = fSub + sChar + sSub + fChar + tSub;
			
			fSub = "";
			sSub = "";
			tSub = "";
		}

		System.out.println(str);
	}

}

- loki.cse August 01, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashSet;
import java.util.Set;

public class VowelsSwitcher {

	
	public static void main(String[] args) {
		Set<Character> vowels = new HashSet<Character>();
		"aeiou".chars().forEach(i -> vowels.add((char) i));
		String str = "continuos";
		char [] chars = str.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			char c = chars[i];
			if(vowels.contains(c)) {
				int j = i+1;
				while(j < (chars.length-i)) {
						j++;
						if(vowels.contains(chars[j])) {
							char temp = chars[i];
							chars[i] = chars[j];
							chars[j] = temp;
							i = j;
							break;
						}
					}
				}
			}
		System.out.println("Orginal String -> "+str);
		System.out.println("After switching -> "+ new String(chars));
		}

}

- Kapil Naidu February 13, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashSet;
import java.util.Set;

public class VowelsSwitcher {


public static void main(String[] args) {
Set<Character> vowels = new HashSet<Character>();
"aeiouAEIOU".chars().forEach(i -> vowels.add((char) i));
String str = "continuos";
char [] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if(vowels.contains(c)) {
int j = i+1;
while(j < (chars.length-i)) {
j++;
if(vowels.contains(chars[j])) {
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
i = j;
break;
}
}
}
}
System.out.println("Orginal String -> "+str);
System.out.println("After switching -> "+ new String(chars));
}

}

- Kapil Naidu February 13, 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