Skip to main content

Posts

Showing posts from December, 2023

Interface in OOPS

 What is interface ? Ans:an interface is a class with all its functions as abstract . interface A{ public void method(); } class One{ public void method(){ System.out.println("Class One method"); } } class Two extends One implements A{ public void method(){ System.out.println("Class Two method"); } } In this example u can see first class is not completed and second class (two) is completed just bcse we are mention implements  Why to use interface? Ans: Interface ka use tab krte hai jab koi page me hame mandatory feature rkhna hai bina uske complete na ho to us case me interface ka use kiya jata hai.

Abstract Oops

 What is Abstract on OOPS ? Ans:  Java mein abstract class ka use karke aap ek class ko define kar sakte hain jiska object directly create nahi kiya ja sakta, lekin jise aap dusre classes ke liye blueprint ke roop mein istemal kar sakte hain. Yeh abstract classes inheritance ke through ek hierarchy ko define karne mein bhi madad karte hain. Niche ek simple example diya gaya hai: // Abstract class abstract class Shape { // Abstract method abstract void draw() ; // Non-abstract method void display() { System.out.println( "This is a shape." ) ; } } // Concrete class extending abstract class class Circle extends Shape { // Implementing abstract method void draw () { System . out .println( "Drawing a circle" ) ; } } // Concrete class extending abstract class class Rectangle extends Shape { // Implementing abstract method void draw() { System.out.println( "Drawing a rectangle" ) ; } } public cla

Test 2

 Question: You have made a smartphone app and want to set its subscription price such that the profit earned is maximised. There are certain users who will subscribe to your app only if their budget is greater than or equal to your price. You will be provided with a list of size N having budgets of subscribers and you need to return the maximum profit that you can earn. Lets say you decide that price of your app is Rs. x and there are N number of subscribers. So maximum profit you can earn is : m*x Sample input 1: 30 20 53 14 Output 60 import   java . util .*; import   java . util . Scanner ; public   class   solution   {      public   static   int   maximumProfit ( int   budget [])   {      Arrays . sort ( budget );          // int maxProfit = 0;          // // Iterate through each possible subscription price and calculate profit          // for (int i = 0; i < budget.length; i++) {          //     int currentProfit = budget[i] * (budget.length - i);          //     maxProfit = Mat

Test Recursion

Given two strings s and t, write a function to check if s contains all characters of t (in the same order as they are in string t). public   class   Solution   {      public   static   boolean   checkSequence ( String   a ,   String   b )   {          /* Your class should be named Solution          * Don't write main().          * Don't read input, it is passed as function argument.          * Return output and don't print it.          * Taking input and printing output is handled automatically.          */           if ( b . length ()   ==   0 ){               return   true ;           }           if ( a . length ()   ==   0 ){               return   false ;           }           //check if the first character match           if ( a . charAt ( 0 )    ==   b . charAt ( 0 )){               return   checkSequence ( a . substring ( 1 ),   b . substring ( 1 ));           }           else {              //  move to the next character              return   checkSequence ( a . sub

Rotate Array

  The input specifies there is 1 test case. For the first test case, the size of the array is 6. The array elements are 5 6 1 2 3 4. We use the arrayRotateCheck function to find the rotation index. Iteration 1: 5 > 6 (not true) Iteration 2: 6 > 1 (true) The current index (i) is 1, and i + 1 is 2. So, the rotation index is 2. The function returns 2. The output for the test case is 2. public   class   Solution   {      public   static   int   arrayRotateCheck ( int []   arr ){          for ( int   i   =   0   ;   i   <   arr . length - 1 ; i ++){              if ( arr [ i ]   >   arr [ i + 1 ]){                  return   i + 1 ;              }                }                 return   0 ; //      The input specifies there is 1 test case. // For the first test case, the size of the array is 6. // The array elements are 5 6 1 2 3 4. // We use the arrayRotateCheck function to find the rotation index. // Iteration 1: 5 > 6 (not true) // Iteration 2: 6 > 1 (true) // The 

Time Complexity

What is time complexity ? Ans: Time complexity not a run time of a code, its totally depends on system configuration, processor, server , input size will increase time complexity. Time complexity ko likhne ke tarike jo hote hai use notation kehte hai. Notation is basically symbol NOTATION OF COMPLEXITY Best Case: kam se kam time me chla gya code best case hota hai Ω (Omega) Ω(1) se likhte hai Average Case: Ek average time jisme code chle  Θ(n+1)/2 Worst Case: Worst case ka mtlb kuch bhi ho jaye is time ko ye exceed nhi kregea O(n) Nested Loop me Time Complexity multiply hoti hai aur n^2 banti hai Wahi Do different loops me N+M hoti hai jisme jiski sabse bdi value hai wo big of O me jati hai O(1) - Constant Time: Operations that take a constant amount of time, regardless of the size of the input data. O(log n) - Logarithmic Time: Algorithms that have a logarithmic time complexity, often seen in binary search or certain tree-based algorithms. This is best time Complexity this is near fr

Quick Sort In JAVA Recursion

  Quick sort ek efficient sorting algorithm hai, jo divide-and-conquer approach ka use karta hai. Quick sort kaam karta hai by partitioning the array into smaller sub-arrays, sorting each sub-array, and then combining them to get the final sorted array. Isme Pivot Element select karega , Pivot element array me se koi bhi element karega aur pivot element lene ke bad usse compare karenge partition karke left side from pivot element is small element and right side from pivot will be bigger element Pivot element hamesa bigger select krenge taaki efficiency behtr aaye