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.

- vladimir.grebennikov December 17, 2014 | Flag Reply
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.

- Anonymous January 05, 2015 | Flag
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..

- ivsmanoj January 07, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

package crackingCodingInterview;

public class StockTraderProfit {

	public static void main(String[] args) {

		int[] trades = {1,2,3,4,-2,-3,1};
		int noOfTrades = 0;

		int maxProfit = tradeMax(trades, noOfTrades);
		System.out.println(maxProfit);
	}

	private static int tradeMax(int[] trades, int noOfTrades) {
		// TODO Auto-generated method stub
		int maxProfit = 0;
		int currentProfit = 0;
		for (int i = 0; i < trades.length; i++) {
			currentProfit = currentProfit + trades[i];
			if (currentProfit <= 0) {
				currentProfit = 0;
			}
			if (currentProfit > maxProfit) {
				maxProfit = currentProfit;
			}
		}

		return maxProfit;

	}
}

- yv1989@outlook.com January 07, 2015 | Flag Reply
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

- ilgor December 17, 2014 | Flag Reply
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;
}

- acts December 18, 2014 | Flag Reply
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);
}

- kshitiz gupta December 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you can use dynamic programming .

- Anonymous December 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int maxprofit(int trades[]){
        int profit = 0;
        for(int i=0;i<trades.length;i++ ){
            if((trades[i] + profit) > profit){
                profit = profit + trades[i];
            }
        }
        return profit;
    }

- silvimasss December 20, 2014 | Flag Reply
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;
}
}

- sgarg002@ucr.edu December 28, 2014 | Flag Reply
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);
	}

- Srishti Singh December 07, 2016 | Flag Reply
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);
}

- Srishti Singh December 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

let a = [-2 ,-3 ,-4 ,-2 ,-3 ,1];
let sum= 0;
let array = [];
for (let i = 0; i < a.length; i++) {
if(a[i] > 0) {
sum += a[i];
} else if(sum > 0) {
array.push(sum);
console.log(sum);
sum = 0;
}
}
array.push(sum);
console.log(array);
let largest = 0;
for (j=0; j<array.length; j++){
if (array[j]>largest) {
largest=array[j];
}
}
console.log(largest)

- Anonymous April 05, 2023 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

let a = [-2 ,-3 ,-4 ,-2 ,-3 ,1];
let sum= 0;
let array = [];
for (let i = 0; i < a.length; i++) {
if(a[i] > 0) {
sum += a[i];
} else if(sum > 0) {
array.push(sum);
console.log(sum);
sum = 0;
}
}
array.push(sum);
console.log(array);
let largest = 0;
for (j=0; j<array.length; j++){
if (array[j]>largest) {
largest=array[j];
}
}
console.log(largest)

- Mukul April 05, 2023 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More