Amazon Interview Question for Backend Developers

Country: United States

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

Looking for coaching on interview preparation?
Visit AONECODE.COM for ONE-TO-ONE private lessons by FB, Google and Uber engineers!

System Design (for candidates of FB, LinkedIn, AMZ, Google and Uber etc)
Algorithms (DP, Greedy, Graph etc. advanced algorithms and clean coding)
Interview questions sorted by companies
Mock Interviews

Ace G, U, FB, Amazon, LinkedIn, MS and other top-tier interviews in weeks of training.

Feel free to email us with any questions. Thanks!

Seems like there are 3 cases:
1. s1 and s2 share no mutual characters. Then there is no way to further minimize the distance.
2. s1 and s2 share a pair of characters on crossed positions, such as 'b' and 'c' in "aabaaac" and "aacaaab". Then swap 'b' and 'c'.
3. s1 and s2 has no crossed pairs but has the same character on different positions. Then swap to align the character.

//@return the pair of indices in s2 to swap with.
    public int[] Swap(String s1, String s2) {
        if(s1.length() > s2.length()) {
            return Swap(s2, s1);
        int swap_idx1 = -1, swap_idx2 = -1;
        for(int i = 0; i < s1.length(); i++) {
            if(s1.charAt(i) != s2.charAt(i)) {
                for (int j = i + 1; j < s1.length(); j++) {
                    if(s1.charAt(i) == s2.charAt(j) && s1.charAt(j) == s2.charAt(i)) {
                        return new int[]{i, j};  //case 2 found
                    } else if(s1.charAt(i) == s2.charAt(j) && s1.charAt(j) != s2.charAt(j)) {
                        swap_idx1 = i;
                        swap_idx2 = j;
                for(int j = s1.length(); j < s2.length(); j++) {
                    if(s1.charAt(i) == s2.charAt(j)) {
                        swap_idx1 = i;
                        swap_idx2 = j;
        if(swap_idx1 == -1) return null; //case1
        else return new int[]{swap_idx1, swap_idx2};

- aonecoding April 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