PayPal Interview Question for SDE-3s


Country: India
Interview Type: Written Test




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

import java.io.BufferedReader;
import java.io.IOException;
import java.lang.*;
import java.util.*;
import java.io.InputStreamReader;  

class Lexograpghic_String_swap{

    public static StringBuilder swap(String str,int  i, int j){
        StringBuilder sb = new StringBuilder(str); 
        sb.setCharAt(i, str.charAt(j)); 
        sb.setCharAt(j, str.charAt(i)); 
        return sb; 
    }

    public static StringBuilder swap_strings_index(String str1,String str2,int  i){
        StringBuilder sb = new StringBuilder(str1); 
        sb.setCharAt(i, str2.charAt(i)); 
        return sb; 
    }



    public static int is_key_available(int[][] arr,int arr_length,int key){
        for(int i=0;i<arr_length;++i){
            if(key== arr[i][0]){
                return arr[i][1];
            }
        }
        return -1;
    }
    public static void main(String[] args) throws IOException {

        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the first String");
        StringBuilder str1=new StringBuilder(sc.next());


        System.out.println("Enter the Second String");
        StringBuilder str2=new StringBuilder(sc.next());

        int length = str1.length();

        System.out.println("Enter the no. of pairs");
        int pairs = sc.nextInt();

        int[][] keys_arr=new int[pairs][2];
        
        
        for (int k=0;k<pairs;k++){
            System.out.println("Enter the "+(k+1)+ " pair with space in between each value");
             String[] str =br.readLine().split(" ");
             keys_arr[k][0]=Integer.parseInt(str[0]);
             keys_arr[k][1]=Integer.parseInt(str[1]);
        }
        for (int i=0;i<=keys_arr[pairs-1][0];i++){

            int index = is_key_available(keys_arr,pairs,i);

            if(index==-1){
                continue;
            }

            if(str1.charAt(i) < str2.charAt(i)){
                    // System.out.println(i+"-in first-loop"+index);
                    if(str1.charAt(i) > str2.charAt(index) && str2.charAt(i) > str2.charAt(index)){
                        // System.out.println(index+"-index first-loop");
                        str2=swap(str2.toString(),i,index);
                        str1=swap_strings_index(str1.toString(),str2.toString(),i);
                    }

                }
            else{
                // System.out.println(i+"-in second-loop"+index);
                if(str1.charAt(i) >= str2.charAt(index) && str2.charAt(i) > str2.charAt(index)){
                    // System.out.println(index+"-index second-loop");
                        str2=swap(str2.toString(),i,index);
                        str1=swap_strings_index(str1.toString(),str2.toString(),i);
                    }
            }
        }

        for(int l=keys_arr[pairs-1][0];l<length;l++){
            if(str1.charAt(l) > str2.charAt(l)){
                str1.setCharAt(l, str2.charAt(l));
            }
        }
        System.out.println(str1);

    }
}

- Borusu Sri Harsha Vardhan May 06, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its not updated one please check the last one

- Borusu Sri Harsha Vardhan May 07, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.BufferedReader;
import java.io.IOException;
import java.lang.*;
import java.util.*;
import java.io.InputStreamReader;  

class Lexograpghic_String_swap{

    public static StringBuilder swap(String str,int  i, int j){
        StringBuilder sb = new StringBuilder(str); 
        sb.setCharAt(i, str.charAt(j)); 
        sb.setCharAt(j, str.charAt(i)); 
        return sb; 
    }

    public static StringBuilder swap_strings_index(String str1,String str2,int  i){
        StringBuilder sb = new StringBuilder(str1); 
        sb.setCharAt(i, str2.charAt(i)); 
        return sb; 
    }



    public static int is_key_available(int[][] arr,int arr_length,int key){
        for(int i=0;i<arr_length;++i){
            if(key== arr[i][0]){
                return arr[i][1];
            }
        }
        return -1;
    }
    public static void main(String[] args) throws IOException {

        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the first String");
        StringBuilder str1=new StringBuilder(sc.next());


        System.out.println("Enter the Second String");
        StringBuilder str2=new StringBuilder(sc.next());

        int length = str1.length();

        System.out.println("Enter the no. of pairs");
        int pairs = sc.nextInt();

        int[][] keys_arr=new int[pairs][2];
        
        
        for (int k=0;k<pairs;k++){
            System.out.println("Enter the "+(k+1)+ " pair with space in between each value");
             String[] str =br.readLine().split(" ");
             keys_arr[k][0]=Integer.parseInt(str[0]);
             keys_arr[k][1]=Integer.parseInt(str[1]);
        }
        for (int i=0;i<=keys_arr[pairs-1][0];i++){

            int index = is_key_available(keys_arr,pairs,i);

            if(index==-1){
                continue;
            }

            if(str1.charAt(i) < str2.charAt(i)){
                    // System.out.println(i+"-in first-loop"+index);
                    if(str1.charAt(i) > str2.charAt(index) && str2.charAt(i) > str2.charAt(index)){
                        // System.out.println(index+"-index first-loop");
                        str2=swap(str2.toString(),i,index);
                        str1=swap_strings_index(str1.toString(),str2.toString(),i);
                    }

                }
            else{
                // System.out.println(i+"-in second-loop"+index);
                if(str1.charAt(i) >= str2.charAt(index) && str2.charAt(i) > str2.charAt(index)){
                    // System.out.println(index+"-index second-loop");
                        str2=swap(str2.toString(),i,index);
                        str1=swap_strings_index(str1.toString(),str2.toString(),i);
                    }
            }
        }

        for(int l=keys_arr[pairs-1][0];l<length;l++){
            if(str1.charAt(l) > str2.charAt(l)){
                str1.setCharAt(l, str2.charAt(l));
            }
        }
        System.out.println(str1);

    }
}

- Borusu Sri Harsha Vardhan May 06, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its not updated one please check the last one

- Borusu Sri Harsha Vardhan May 07, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.BufferedReader;
import java.io.IOException;
import java.lang.*;
import java.util.*;
import java.io.InputStreamReader;  

class Lexograpghic_String_swap{

    public static StringBuilder swap(String str,int  i, int j){
        StringBuilder sb = new StringBuilder(str); 
        sb.setCharAt(i, str.charAt(j)); 
        sb.setCharAt(j, str.charAt(i)); 
        return sb; 
    }

    public static StringBuilder swap_strings_index(String str1,String str2,int  i){
        StringBuilder sb = new StringBuilder(str1); 
        sb.setCharAt(i, str2.charAt(i)); 
        return sb; 
    }



    public static int is_key_available(int[][] arr,int arr_length,int key){
        for(int i=0;i<arr_length;++i){
            if(key== arr[i][0]){
                return arr[i][1];
            }
        }
        return -1;
    }
    public static void main(String[] args) throws IOException {

        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the first String");
        StringBuilder str1=new StringBuilder(sc.next());


        System.out.println("Enter the Second String");
        StringBuilder str2=new StringBuilder(sc.next());

        int length = str1.length();

        System.out.println("Enter the no. of pairs");
        int pairs = sc.nextInt();

        int[][] keys_arr=new int[pairs][2];
        
        
        for (int k=0;k<pairs;k++){
            System.out.println("Enter the "+(k+1)+ " pair with space in between each value");
             String[] str =br.readLine().split(" ");
             keys_arr[k][0]=Integer.parseInt(str[0]);
             keys_arr[k][1]=Integer.parseInt(str[1]);
        }
        for (int i=0;i<=keys_arr[pairs-1][0];i++){

            int index = is_key_available(keys_arr,pairs,i);

            if(index==-1){
                continue;
            }

            if(str1.charAt(i) < str2.charAt(i)){
                    // System.out.println(i+"-in first-loop"+index);
                    if(str1.charAt(i) >= str2.charAt(index) && str2.charAt(i) >= str2.charAt(index)){
                        // System.out.println(index+"-index first-loop");
                        str2=swap(str2.toString(),i,index);
                        // System.out.println(index+"-swap-index in str2 in loop-1 "+str2);
                        str1=swap_strings_index(str1.toString(),str2.toString(),i);
                        // System.out.println(i+"-swap-index in str1 and str2 in loop-1 "+str1);
                    }
                }
            else{
                // System.out.println(i+"-in second-loop"+index);
                if(str1.charAt(i) >= str2.charAt(index) && str2.charAt(i) > str2.charAt(index)){
                    // System.out.println(index+"-index second-loop");
                        str2=swap(str2.toString(),i,index);
                        // System.out.println(index+"-swap-index  in str2 in loop-2 "+str2);
                        str1=swap_strings_index(str1.toString(),str2.toString(),i);
                        // System.out.println(i+"-swap-index in str1 and str2 in loop-2 "+str1);
                    }
                    else if(str1.charAt(i) >= str2.charAt(index) && str2.charAt(i) <= str2.charAt(index)){
                        str1=swap_strings_index(str1.toString(),str2.toString(),i);
                        // System.out.println(i+"-swap-index in str1 and str2 in loop-3 "+str1);
                    }
            }
        }

        for(int l=keys_arr[pairs-1][0];l<length;l++){
            if(str1.charAt(l) > str2.charAt(l)){
                str1.setCharAt(l, str2.charAt(l));
            }
        }
        //System.out.println(str2);
        System.out.println(str1);

    }
}

- Borusu Sri Harsha Vardhan May 06, 2020 | 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