Programming interview - written tests

ArnoDorian

Well-known member
  • Jul 25, 2017
    890
    483
    63
    Reverse String in Java

    Reverse a given String in java without StringBuffer/Builder..
    Intern Technical interview @ Virtusa
     

    shw7

    Well-known member
  • May 30, 2009
    2,150
    171
    113
    London, England
    Reverse a given String in java without StringBuffer/Builder..
    Intern Technical interview @ Virtusa

    class riverse{
    public static void main(String args[]){

    String word = "Test";
    //riverse = riverse(word);
    System.out.println(riverse(word));
    }

    public static String riverse(String source){

    String riverse= "";
    for(int i= source.length() - 1; i>=0; i--){
    riverse = riverse + source.charAt(i);
    }
    return riverse;
    }
    }
     

    EMINƎM

    Well-known member
  • Aug 15, 2011
    5,123
    727
    113
    Rochester Hills, Detroit, Michigan, U.S.
    class riverse{
    public static void main(String args[]){

    String word = "Test";
    //riverse = riverse(word);
    System.out.println(riverse(word));
    }

    public static String riverse(String source){

    String riverse= "";
    for(int i= source.length() - 1; i>=0; i--){
    riverse = riverse + source.charAt(i);
    }
    return riverse;
    }
    }



    how do we optimize this code?


    Code:
    class riverse{
        public static void  main(String args[]){
        
            String word = "Test";
            //riverse = riverse(word);
            System.out.println(riverse(word));
        }
        
        public static String riverse(String source){
        
            StringBuilder  riverse= new StringBuilder("");
            
            for(int i = source.length()-1; i>=0; i--) {
                 riverse.append(source.charAt(i));
            }
    
             return  riverse.toString();
        }
    }
    changing "riverse" String to a StringBuilder so that it wont fill up the String constant pool every time when the "for loop" continues
    Since String is an immutable and StringBuilder is not, StringBulder can append to the same object in the memory every time, which takes less memory space





    how to change the code to test a palindrome?


    Code:
    class riverse{
        public static void  main(String args[]){
        
            String word = "sourcecruos";
            //riverse = riverse(word);
            System.out.println(riverse(word));
        }
        
        public static boolean riverse(String source){
        
            StringBuilder  riverse= new StringBuilder("");
            
            for(int i = source.length()-1; i>=0; i--) {
                 riverse.append(source.charAt(i));
            }
    
             return  source.equals(reversedString.toString());
        }
    }
     

    saja

    Well-known member
  • Jan 8, 2007
    16,140
    2
    10,770
    113
    Home Sweet Home
    woxzone (woxzone.com) කියන company එකේ ASP .NET MVC interview practical test එකට අහනේ මොක්ක ද ? company එක නම් upset ඇති, එත් නිවාඩු දාල යන්නේ , එක නිසා PASS වෙන්න ඕනේ , please දන්නා කෙනෙක් ඉන්නවා නම් support එකක් දෙන්න , practical ටෙස්ට් එකට අහන්නේ මොක්ක ද කියල
     

    thinking_guy

    Well-known member
  • Apr 16, 2011
    4,718
    2,737
    113
    EMINƎM;24264183 said:
    String a = "thinking";
    String b = "guy";

    a = a + b; //thinkingguy
    b = a.substring(0, a.length()-b.length()); //thingking
    a = a.substring(b.length()); //guy

    System.out.println("a " + a);
    System.out.println("b " + b);

    You are hired son. :D
     

    saja

    Well-known member
  • Jan 8, 2007
    16,140
    2
    10,770
    113
    Home Sweet Home
    look here carefully

    if a = 10, b = 20;

    a = a + b; -- line 01
    b = a - b; -- line 02
    a = a - b; -- line 03

    -- line 01 output a = a + b = 10+ 20 = 30 , (still b = 20)
    -- line 02 output b = a - b = 30 - 20 = 10, (still a = 30)
    -- line 03 output a = a - b = 30 - 10 = 20

    final output a = 20 , b = 10

    still cannot understand :sorry:

    this is valid and no need for Xor swapping



    then how to get value of b(after swaping)
     

    windows_ubuntu

    Well-known member
  • Jun 2, 2018
    16,751
    24,851
    113
    Fantasy
    look here carefully

    if a = 10, b = 20;

    a = a + b; -- line 01
    b = a - b; -- line 02
    a = a - b; -- line 03

    -- line 01 output a = a + b = 10+ 20 = 30 , (still b = 20)
    -- line 02 output b = a - b = 30 - 20 = 10, (still a = 30)
    -- line 03 output a = a - b = 30 - 10 = 20

    final output a = 20 , b = 10

    still cannot understand :sorry:

    this is valid and no need for Xor swapping


    understood. thanx brother:love: