Monday, August 12, 2024

Coding Interview Programs

Coding Interview Programs:

FAQ IP Ascending order of array :

                              int a[] = {10,3,5,4};

Ascending order :    3,4,5,10

 

package FileHanldingBasics;

public class Ascendingorder {

               public static void main(String[] args)

               {

                              int a[] = {10,3,5,4};

                              //         0  1 2 3

                             

                              // Asc order :   3,4,5,10

                              for(int i=0;i<a.length;i++)// i=0

                              {

                                             for(int j=i+1;j<a.length;j++)// j=i+1, 1

                                             {

                                                            int temp;

                                                            if(a[i] > a[j]) //  a[0] > a[1]

                                                            { //  swap numbers 10>3 true , swap

                                                                           temp = a[i];

                                                                           a[i] = a[j];

                                                                           a[j] = temp;

                                                            }                                                          

                                             }

                              }

                             

                              //   display array

                              for(int i=0;i<=a.length-1;i++)

                              {

                                             System.out.println(a[i]);

                              }

                             

               }

}

o/p:

3

4

5

10

IP FAQ Descending order of array :

                              int a[] = {10,3,5,4};

Desc :  10,5,4,3

-------------------------

IP FAQ 1st , 2nd , 3rd max value in array

package FileHanldingBasics;

public class Ascendingorder {

               public static void main(String[] args)

               {

                              int a[] = {10,3,5,4};

                              //         0  1 2 3

                             

                              // Asc order :   3,4,5,10

                              for(int i=0;i<a.length;i++)// i=0

                              {

                                             for(int j=i+1;j<a.length;j++)// j=i+1, 1

                                             {

                                                            int temp;

                                                            if(a[i] > a[j]) //  a[0] > a[1]

                                                            { //  swap numbers 10>3 true , swap

                                                                           temp = a[i];

                                                                           a[i] = a[j];

                                                                           a[j] = temp;

                                                            }                                                          

                                             }

                              }

                             

                              //   display array

                              for(int i=0;i<=a.length-1;i++)

                              {

                                             System.out.println(a[i]);

                              }

                             

                              //Asc order :   3,4,5,10,

                              //              0 1 2  3

                             

                              // 1st max value

                              int le = a.length;

                              //       4

                             

                              System.out.println("Max val ="+ a[3]);

                              System.out.println("1st Max val ="+ a[le-1]);

                              System.out.println("2nd Max val ="+ a[le-2]);

                              System.out.println("3rd Max val ="+ a[le-3]);

                              System.out.println("4th Max val ="+ a[le-4]);

                             

int nthMax = 3;

                              int val = a[le-nthMax];

                              System.out.println(val)

               }

}

-----------

--------------

package FileHanldingBasics;

public class Desendingorder {

               public static void main(String[] args)

               {

                              int a[] = {10,3,5,4};

                              //         0  1 2 3

                             

                              // Des order :   10, 5 4 3

                              for(int i=0;i<a.length;i++)// i=0

                              {

                                             for(int j=i+1;j<a.length;j++)// j=i+1, 1

                                             {

                                                            int temp;

                                                            if(a[i] < a[j]) //  a[0] > a[1]

                                                            { //  swap numbers 10>3 true , swap

                                                                           temp = a[i];

                                                                           a[i] = a[j];

                                                                           a[j] = temp;

                                                            }                                                          

                                             }

                              }

                             

                              //   display array

                              for(int i=0;i<=a.length-1;i++)

                              {

                                             System.out.println(a[i]);

                              }

                             

               }

}

o/p:

10

5

4

3

-------------------

 10 5 4 3

HW IP  get 1st,2nd min, 3rd min value   from desc order?

HW IP  get 1st,2nd min, 3rd min value   from Asc order?

HW IP  WAP to develop a method to get nth max value  from desc order?

NthMaxValInArra(int nth)

{

}

NthMaxValInArray(1) --> 1st max

NthMaxValInArray(2) --> 2nd max

HW IP  WAP to develop a method to get nth min value  from desc order?

NthMinValInArray(int nth)

{

}

NthMinValInArray(1) --> 1st min

NthMinValInArray(2) --> 2nd min

FAQ IP: FAQ Get all File names from given Folder :

package FileHanldingBasics;

import java.io.File;

public class GetallFileNames {

               public static void main(String[] args) {

                             

                              //  Folders and File --> File - class

                             

                              File  f  = new File("C:\\brahma\\Practise\\SelniumPractiseNew\\March52024MyWorkspace\\SampleJavaProject2\\src\\FileHanldingBasics");

                              //  Can be used to handle folders. files

                             

              

                             

                              //  to get list of files -   we have list()

                              String s[] = f.list();                                         

//                           String s =  f.list();// String variable // Dont use

                             

                              // count of all files

                              int filesCnt =s.length;

                              System.out.println("files count="+filesCnt);

                             

                              // display all files

                              for(String eachFileName : s)

                              {

                                             System.out.println(eachFileName);

                              }

                             

               }

}

o/p:

files count=11

appendDataToExistingFile.java

Ascendingorder.java

DataAppend.txt

Desendingorder.java

GetallFileNames.java

ReadingAllDataFromNotePadFile.java

ReadingNotePadFile.java

Sample.txt

TestData.txt

WriteDatatoNotePadFile.java

WriteDatatoNotePadFile2.java

// HW WAP to get ‘txt’ files count  and display it

// HW WAP to get ‘excel’ files count and display it-.xlsx or xls                  

// HW WAP to get ‘java’ files count and display it --  .java

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...