Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

There exists a O(NM) solution. N is size of list and M is average size of each string in list.
M exist here because we need to reverse string and check if its palindrome. Here is my solution:

public class PalindromeConcatenation {
	
	
	private static Set<String> buildSet(ArrayList<String> listOfWords){
	

		return new HashSet<>(listOfWords);
	}
	
	public static void findPalindromes(ArrayList<String> listOfWords){
		
		//for ex: a string "shiva" store "avihs" , "vihs" and "avih" are potential strings
		//than can be present in the list can be concatenated to form palindrome
		boolean flag=false;
		Set<String> hashSet=buildSet(listOfWords);
		for(String i:listOfWords){
			String reverse= new StringBuilder(i).reverse().toString();
			
			if(hashSet.contains(reverse)){
				flag=true;
				System.out.println(i+" and "+reverse+" are palindromes");
			}
			
			String reverseWithoutFirstChar=reverse.substring(1);
			if(hashSet.contains(reverseWithoutFirstChar)){
				flag=true;
				System.out.println(i+" and "+reverseWithoutFirstChar+" are palindromes");
			}
			
			String reverseWithoutLastChar=reverse.substring(0,reverse.length()-1);
			//System.out.println(reverseWithoutLastChar);
			if(hashSet.contains(reverseWithoutLastChar)){
				flag=true;
				System.out.println(i+" and "+reverseWithoutLastChar+" are palindromes");
			}
		}
		if(!flag){
			System.out.println("No strings that can be concatenated to form a palindrome are present");
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ArrayList<String> listOfWords=new ArrayList<>();
		listOfWords.add("shiva");
		listOfWords.add("and");
		listOfWords.add("are");
		listOfWords.add("vihs");
		listOfWords.add("avihs");
		findPalindromes(listOfWords);	
	}

}

- sivapraneethalli November 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* ZoomBA
Note that  sivapraneethalli is kind of correct.
A much better way to achieve this is to see this :
xy is a palindrome iff :
 y = ( x ** -1 ) or 
 y = ( x[0:-2] ** -1 )   

As an example : 
1.  x = abc 
    y = cba 
2.  x = abc 
    y = ba 

Thus, a better algo is to store the potential y's as keys to 
to a map, and value being that of x in a list.
Hence, we can get it done in two passes.     
*/

def find_pair_palindromes( strings ){
  map = fold( strings , dict() ) -> {
    x = $.o 
    key = str( x ** -1 )
    $.p[ key ] = $.i // store index
    if ( size(x) > 1 ){
      key = str( x[0:-2] ** -1 )
      $.p[ key ] = $.i // store index
    }
    $.p // return 
  }
  fold ( strings ) -> { 
    y = $.o 
    if ( y @ map ){ printf( '%s %s\n', strings[ map[y] ] , y ) }
  }
}

listOfWords = list(  "shiva" ,  "and" , "are" , "vihs" , "avihs" )
find_pair_palindromes( listOfWords )

- NoOne November 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean hasTwoPalindrome(List<String> list) {

Set<String> set = list.parallelStream()
.map(w -> new StringBuilder(w).reverse().toString())
.collect(Collectors.toSet());

for (String word : list) {
if (set.contains(word)) {
return true;
}
}
return false;
}

- Anonymous November 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean hasTwoPalindrome(List<String> list) {

        Set<String> set = list.parallelStream()
                .map(w -> new StringBuilder(w).reverse().toString())
                .collect(Collectors.toSet());

        for (String word : list) {
            if (set.contains(word)) {
                return true;
            }
        }
        return false;
    }

- Anonymous November 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

namespace T2
{
    class Program
    {
        static private string[] strings = { "one", "two", "three", "owt", "12345", "4321" };

        private static bool IfPalindrom(string prototype)
        {
            char[] chproto = prototype.ToCharArray();
            bool isproto = true;
            for (int i=0; i<chproto.Length/2; ++i)
            {
                if (chproto[i] != chproto[chproto.Length - 1 -i])
                {
                    isproto = false;
                    break;
                }
            }
            return isproto;
        }

        static void Main(string[] args)
        {
            int n = strings.Length;
            for (int i=0; i<n; ++i)
            {
                for (int j=0; j<n; ++j)
                {
                    char first = strings[i][0];
                    char last = strings[j][strings[j].Length - 1];
                    if (first == last)
                    {
                        string prototype = strings[i] + strings[j];
                        if (IfPalindrom(prototype))
                        {
                            Console.WriteLine(strings[i] + "  " + strings[j]);
                        }
                    }
                }
            }
        }
    }
}

- Student November 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def check_any_concat_is_palindrome(list_strings):

    def is_palindrome(s):
        return s == s[::-1]

    reversed_list = map(lambda x: x[::-1], list_strings)

    for word in list_strings:
        for rev in reversed_list:
            if word.startswith(rev):
                if len(word) == len(rev):
                    return True
                if is_palindrome(word[-len(rev) + 1:]):
                    return True
    return False

- tkachoff November 28, 2016 | 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