Amazon Interview Question for Backend Developers

Country: United States

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

Another way is stating this problem is to say whether two strings are permutations of each other. I present two solutions. One using sorting which takes O(nlogn) where n = the number of characters/letters in the given word. Other approach is to use a hash table and maintain counts of letters. If the counts match, then the target string can be formed by swapping the characters in the original string.

Elegant solutions in python:

# O(nlogn) time for sort
def canBeSwappedUsingSort(originalString, targetString):
    return sorted(originalString) == sorted(targetString)

# O(n) time
def canBeSwappedUsingHash(originalString, targetString):
    from collections import Counter
    return Counter(originalString) == Counter(targetString)

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

1. First check if length of both the string is same or not.
2. traverse through target string and store the count of each char in a map/array.
3. now traverse through another string and decrement the count of the char from original store.
4. At the end if the store has a count of 0 for all the char return value true else false.

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


- Anonymous March 01, 2018 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


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


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