Nagarro Interview Question for Interns

Country: India

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

O(n)

``````private int TradeMax(params int[] profits)
{
int maxProfit = 0;
int currentStreak = 0;
for (int i = 0; i < profits.Length; i++)
{
currentStreak += profits[i];
if (currentStreak < 0) currentStreak = 0;
if (currentStreak > maxProfit) maxProfit = currentStreak;
}
return maxProfit;
}``````

Each time current profit streak is less than zero we can assume that we should've started trading later. This means that we can assign zero to current streak and assume that we're starting trading from the next point.

Comment hidden because of low score. Click to expand.
-1
of 1 vote

This will not work when the input array likewise --> 2,4,5,-2,4,-4,3 correct answer is 12, your code will return 11.

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

The code looks good.. 2+4+5-2+4 = 13(max value), later -4, +3 will not change this max value..

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

``````package crackingCodingInterview;

public static void main(String[] args) {

System.out.println(maxProfit);
}

// TODO Auto-generated method stub
int maxProfit = 0;
int currentProfit = 0;
for (int i = 0; i < trades.length; i++) {
if (currentProfit <= 0) {
currentProfit = 0;
}
if (currentProfit > maxProfit) {
maxProfit = currentProfit;
}
}

return maxProfit;

}
}``````

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

import java.util.Scanner;

public class Driver
{

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

int min = Integer.MIN_VALUE;
int maxProfit = 0;

String[] input = stdIn.nextLine().split(" ");

for (int i = 0; i < input.length && min < Integer.parseInt(input[i]); i++)
{
if (Integer.parseInt(input[i]) > 0)
{
maxProfit += Integer.parseInt(input[i]);
//min = Integer.parseInt(input[i]);
min = 0;
}
}
System.out.println(maxProfit);
} // end of main
} // end of class

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

``````#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> Numbers;
vector<int>::iterator itr;
int N,num,i;
int MaxSum = 0, SumSofar = 0;
cout<<"Enter Numbers of integeres: "<<endl;
cin>>N;
cout<<"Enter "<<N<<" Integers"<<endl;
for(i=1;i<=N;i++){
cin >>num;
Numbers.push_back(num);
}
for(itr=Numbers.begin();itr!=Numbers.end();++itr)
cout<<*itr<<" ";

for(itr=Numbers.begin();itr!=Numbers.end();++itr){
SumSofar = SumSofar + *itr;
if(SumSofar < 0){
SumSofar = 0;
}else if(MaxSum<SumSofar){
MaxSum = SumSofar;
}
}
cout<<" "<<endl;
cout<<"Max Sum = "<<MaxSum<<endl;
return 0;
}``````

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

``````#include<stdio.h>
#include<stdlib.h>
#include<string.h>

main()
{
int a[7] = {1,2,3,4,-2,-3,1};
int i,j,k,max =0,total;
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
total = 0;
for(k=i;k<j;k++)
{
total = a[k] + total;
if(max < total)
{
max = total;
}
}
}
}
printf("max %d",max);
}``````

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

you can use dynamic programming .

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

``````public static int maxprofit(int trades[]){
int profit = 0;
}
}
return profit;
}``````

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

``````import java.util.*;
public class MaxProfit {
public static void main(String[] args){
int input[] = {1,2,3,4,-2,-3,1};
System.out.println(Max(input));
}
public static int Max(int array[]){
int maximum=0;
int sum[] = new int[array.length];
if(array[0] > 0){
maximum = array[0];
}
sum[0]= maximum;
for(int i = 1;i < array.length;i++){
sum[i] = array[i]+sum[i-1];
if (sum[i] > maximum){
maximum = sum[i];
}
}
return maximum;
}
}``````

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

``````static void findMaxProfit(int trades, int[] profitLossArray){

int sum = profitLossArray[0];
int maxSum = sum;

for(int i = 1; i<profitLossArray.length; i++) {
if((sum <0) && ((sum+profitLossArray[i])>sum)){
sum = profitLossArray[i];
}
else
sum = sum + profitLossArray[i];

if(maxSum<sum){
maxSum = sum;
}
}
System.out.println(maxSum);
}``````

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

static void findMaxProfit(int trades, int[] profitLossArray){

int sum = profitLossArray[0];
int maxSum = sum;

for(int i = 1; i<profitLossArray.length; i++) {
if((sum <0) && ((sum+profitLossArray[i])>sum)){
sum = profitLossArray[i];
}
else
sum = sum + profitLossArray[i];

if(maxSum<sum){
maxSum = sum;
}
}
System.out.println(maxSum);
}

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.

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.