paytm interview questions

  1. Problem:
Reversing the order of terms and operators in an algebraic expression
  e.g for input "5+6*7-60" the output should be "60-7*6+5"

Solution is as follows:

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

String str=args[0];
String reverse="";

int pos=str.length()-1;

String term="";

     for(int k=0;k<str.length();k++)
    {
     char ch=str.charAt(pos--);

          switch(ch)
         {
          case '-':
          case '+':
          case '/':
          case '*':
          reverse=reverse+term+ch;
          term="";
          break;
          default:
          term=ch+term;
          }//switch

     }//for

System.out.println("reverse is "+(reverse+term));

}//main
}//class

The above program should be run on command line as 
 java Main "5+6*7-60". 
Note down the double quotes. 

If you want to run the program in an IDE like eclipse then right click on the program choose run as then run configuration and then click on arguements tab and then write the input in the program arguements.

===================================================================
Program 2.
The cost of a stock on each day is given in an array, find the max profit that you can make by buying and selling in those days.
For example, if the given array is {100,180,260,310,40,535,695} the maximum profit can earned by buying on day 0, selling on day 3. Again buy on day 4 and sell on day 6. If the given array of prices is sorted in decreasing order, then profit cannot be earned at all.

Solution:

 import java.util.*;

class BuySell
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);

System.out.println("Enter the size of the array");
int size=in.nextInt();

int arr[]=new int[size];

    for(int k=0;k<size;k++)
    {
    System.out.println("Enter the stock price at day"+k);
    arr[k]=in.nextInt();
    }

    int buyDay=0;
    int sellDay=0;

    int k=0;
   
    int m=k+1;
    int nextBuyDay=m;
   
    while(m<size)
    {   
    int big=arr[k];
    buyDay=k;

    int flag=0;

        while(m<size)
        {   
            if(arr[m]>big)
            {
            big=arr[m];
            sellDay=m;
            }
            else
            {
            nextBuyDay=m;
            flag=1;
            break;
            }
        m++;
        k++;
        }
       
    if(buyDay!=sellDay)
    System.out.println("buy at day "+buyDay+" and sell at day "+sellDay);
    if(flag==1)
    {
    k=nextBuyDay;
    m=k+1;   
    }
   

}//while       

}

}
==========================


1 comment: