US Interview Question for Software Engineers


Team: Amazon Prime
Country: United States
Interview Type: In-Person




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

You can just use a pointer to the first char and last char in the string. increment the first and decrement the last until they meet, comparing at each iteration. If they are not equal at any time, return False (is not palindrome).

in python:

def is_palindrome(word):
	ptr_front = 0
	ptr_back = len(word) - 1
	while ptr_front < ptr_back:
		if word[ptr_front] != word[ptr_back]:
			return False
		ptr_front += 1
		ptr_back -= 1
	return True

This solution is O(n) runtime, and works in-place thus being preferable to methods which use a stack or a second string with the same runtime. This could be done in fewer lines if i used something like

for i in range(0, len(word)):
	if word[i] != word[len(word)-i]:
		return false

but pointers are easier to visualize

Avoid using language-specific structures like stringbuilder if possible, and for problems like this you shouldn't use pre-existing methods to skip major portions of the logic they are testing you on. Using the stringbuilder's reverse method leaves you at the mercy of not knowing the space or time complexity, as well as leaving your interviewer underwhelmed.

- Javeed March 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

text = 'Madam'
def reverse(text):
return text[::-1]
reverse(text)

What about above simple python code?

- Zaman_Fatma April 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

text = 'madam'
def reverse(text):
return text[::-1]
reverse(text)

what about above simple python code?

- Fatma April 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

text = 'madam'
def palindrome_text(text):
return text[::-1]
palindrome_text(text)

What about simple python code?

- Zaman_Fatma April 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python code for palindrome

text = 'Madam'
def reverse(text):
return text[::-1]
reverse(text)

What about this simple and fast code?

- Zaman_Fatma April 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Console.WriteLine("The given string is: ");

string s = "Madam";
int length = s.Length;
int i = 0, j = length -1;
int count = 0;
bool _ispalin = true;
while(i<j)
{
if (s[i].ToString().ToUpper() == s[j].ToString().ToUpper())
{
// continue..
count++;
i++;
j--;
}
else
{
_ispalin = false;
break;
}
}

if (_ispalin)
{
Console.WriteLine("Palindrome");
}
else
{
Console.WriteLine("not palindrome");
}

- Arch May 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

import java.util.Stack;

public class StackDemo {

static void stackPush(Stack st, Character a){
st.push(new Character(a));
}
static void stackPop(Stack st){

}
static boolean isPalindrome(Stack st, int size)
{
int i=0;
for(i=0; i<=(size - (i+1)); i++)
{
System.out.println(" i size " + i + " " + (size - (i+1)));
System.out.println(" ---- " +st.elementAt(i) + " " + st.elementAt(size - (i+1)));

if(st.get(i).equals(st.elementAt(size - (i+1))))
{

}
else
return false;

}
return true;
}
public static void main(String args[])
{
Stack<Character> st = new Stack<Character>();
stackPush(st,'b');
stackPush(st,'a');
stackPush(st,'n');
stackPush(st,'a');
stackPush(st,'a');
stackPush(st,'n');
stackPush(st,'a');
stackPush(st,'b');
int sizeSt = st.size();
System.out.println(" Size " + sizeSt);
boolean result = isPalindrome(st,sizeSt);
System.out.println(" Result " + result);

}
}

- Smily May 29, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Stack;

public class StackDemo {

static void stackPush(Stack st, Character a){
st.push(new Character(a));
}
static void stackPop(Stack st){

}
static boolean isPalindrome(Stack st, int size)
{
int i=0;
for(i=0; i<=(size - (i+1)); i++)
{
System.out.println(" i size " + i + " " + (size - (i+1)));
System.out.println(" ---- " +st.elementAt(i) + " " + st.elementAt(size - (i+1)));

if(st.get(i).equals(st.elementAt(size - (i+1))))
{

}
else
return false;

}
return true;
}
public static void main(String args[])
{
Stack<Character> st = new Stack<Character>();
stackPush(st,'b');
stackPush(st,'a');
stackPush(st,'n');
stackPush(st,'a');
stackPush(st,'a');
stackPush(st,'n');
stackPush(st,'a');
stackPush(st,'b');
int sizeSt = st.size();
System.out.println(" Size " + sizeSt);
boolean result = isPalindrome(st,sizeSt);
System.out.println(" Result " + result);


}
}

- Smily_Chawla May 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Stack;

public class StackDemo {

static void stackPush(Stack st, Character a){
st.push(new Character(a));
}
static void stackPop(Stack st){

}
static boolean isPalindrome(Stack st, int size)
{
int i=0;
for(i=0; i<=(size - (i+1)); i++)
{
System.out.println(" i size " + i + " " + (size - (i+1)));
System.out.println(" ---- " +st.elementAt(i) + " " + st.elementAt(size - (i+1)));

if(st.get(i).equals(st.elementAt(size - (i+1))))
{

}
else
return false;

}
return true;
}
public static void main(String args[])
{
Stack<Character> st = new Stack<Character>();
stackPush(st,'b');
stackPush(st,'a');
stackPush(st,'n');
stackPush(st,'a');
stackPush(st,'a');
stackPush(st,'n');
stackPush(st,'a');
stackPush(st,'b');
int sizeSt = st.size();
System.out.println(" Size " + sizeSt);
boolean result = isPalindrome(st,sizeSt);
System.out.println(" Result " + result);


}
}

- Smily_Chawla May 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Stack;

public class StackDemo {

static void stackPush(Stack st, Character a){
st.push(new Character(a));
}
static void stackPop(Stack st){

}
static boolean isPalindrome(Stack st, int size)
{
int i=0;
for(i=0; i<=(size - (i+1)); i++)
{
System.out.println(" i size " + i + " " + (size - (i+1)));
System.out.println(" ---- " +st.elementAt(i) + " " + st.elementAt(size - (i+1)));

if(st.get(i).equals(st.elementAt(size - (i+1))))
{

}
else
return false;

}
return true;
}
public static void main(String args[])
{
Stack<Character> st = new Stack<Character>();
stackPush(st,'b');
stackPush(st,'a');
stackPush(st,'n');
stackPush(st,'a');
stackPush(st,'a');
stackPush(st,'n');
stackPush(st,'a');
stackPush(st,'b');
int sizeSt = st.size();
System.out.println(" Size " + sizeSt);
boolean result = isPalindrome(st,sizeSt);
System.out.println(" Result " + result);


}
}

- Smily May 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

is st.elementAt some kind of an inbuilt function. This looks like we are not accessing the top element always. Os this a good idea to do when we have to take care of the properties of stack?

- Megha Maheshwari December 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Stack;

public class checkPaliandromeUsingStack {

//public static void main(String[] args) {

Stack <Character> st = new Stack<Character>();
public boolean isPaliandrome(String input) {
for(char s:input.toCharArray()) {
st.push(new Character(s));

}
String returnString="";
while(!st.isEmpty()) {

returnString+=st.pop().toString();
}
return(returnString.equals(input)) ;
}

public static void main(String args[]){
checkPaliandromeUsingStack palStack=new checkPaliandromeUsingStack();
if(palStack.isPaliandrome("malayalam")) {
System.out.println("Paliandrome");
}
else {
System.out.println("Not a paliandrome");
}
}
}

- APV August 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Stack;

public class checkPaliandromeUsingStack {

	//public static void main(String[] args) {
	   
		Stack <Character> st = new Stack<Character>();
		public boolean isPaliandrome(String input) {
			for(char s:input.toCharArray()) {
				st.push(new Character(s));
				
			}
			String returnString="";
			while(!st.isEmpty()) {
				
				returnString+=st.pop().toString();
			}
			return(returnString.equals(input)) ;
		}
		
	public static void main(String args[]){
		checkPaliandromeUsingStack palStack=new checkPaliandromeUsingStack();
		if(palStack.isPaliandrome("malayalam")) {
			System.out.println("Paliandrome");
		}
		else {
			System.out.println("Not a paliandrome");
		}
	}
	}

//------------------------------------------------

- APV August 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What would exactly be the complexity of this. I am assuming it would be the length of the string since you need that many push and pop

- Megha Maheshwari December 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Stack;

public class checkPaliandromeUsingStack {

	//public static void main(String[] args) {
	   
		Stack <Character> st = new Stack<Character>();
		public boolean isPaliandrome(String input) {
			for(char s:input.toCharArray()) {
				st.push(new Character(s));
				
			}
			String returnString="";
			while(!st.isEmpty()) {
				
				returnString+=st.pop().toString();
			}
			return(returnString.equals(input)) ;
		}
		
	public static void main(String args[]){
		checkPaliandromeUsingStack palStack=new checkPaliandromeUsingStack();
		if(palStack.isPaliandrome("malayalam")) {
			System.out.println("Paliandrome");
		}
		else {
			System.out.println("Not a paliandrome");
		}
	}
	}

- APV August 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

returnString+=st.pop().toString(); will always create a new string object

- rew September 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static bool IsPalindrome(string input)
        {
            if (input == null) return false;
            if (input == string.Empty) return false;
            if (input.Length == 1) return true;
            if (input.Length == 2 && input[0] == input[1]) return true;
            if (input.Length == 2 && input[0] != input[1]) return false;

            var isit = false;

            for (int i= 0; i < input.Length/2; i++)
            {
                if (input[i] != input[input.Length -1 - i]) isit = false;
                else isit = true;
            }

            return isit;
            
        }

- ajay.majgaonkar February 17, 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