Walmart Labs Interview Question for Java Developers

Country: India
Interview Type: Written Test

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

Typical DP problem, developing a mathematical equation should solve

Assume all the diamonds are in array, arr[] whose size is N

Maximum he can collect with N dragons is
MaxDiam(N)= Math.max{ arr[N-1] + MaxDiam(N-2), MaxDiam(N-1)}

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

Why can't He take "5+2+4" =11?

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

Why can't he take "5+2+4" = 11?

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

``````var input = [1,2,3,4,5];

var output = input.reduce(function(prev,e) {
return [prev[1],prev[2],Math.max(prev[0],prev[1])+e];
},[0,0,0]);

console.log(output[2]);``````

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

``````public class DiamondCollectionPuzzle {
private static int[] input= {1, 2, 3, 4, 5, 6, 7};
public static void main(String[] args) {
PosssibleDiamondCount possibleDiamonds = collectDiamonds(0);
System.out.println(Math.max(possibleDiamonds.maxExcludingCurrent, possibleDiamonds.maxIncludingCurrent));
}
private static PosssibleDiamondCount collectDiamonds(int startIndex) {
if(startIndex == input.length-1){
PosssibleDiamondCount possibleDiamondCount = new PosssibleDiamondCount();
possibleDiamondCount.maxIncludingCurrent = input[startIndex];
possibleDiamondCount.maxExcludingCurrent = 0;
return possibleDiamondCount;
}else{
PosssibleDiamondCount nextPossibleDiamondCount = collectDiamonds(startIndex + 1);
PosssibleDiamondCount currPossibleDiamondCount = new PosssibleDiamondCount();
currPossibleDiamondCount.maxIncludingCurrent = input[startIndex] + nextPossibleDiamondCount.maxExcludingCurrent;
currPossibleDiamondCount.maxExcludingCurrent = nextPossibleDiamondCount.maxIncludingCurrent;
return currPossibleDiamondCount;
}
}

/**
* Data structure to hold possible diamonds including current and excluding current
*/
private static class PosssibleDiamondCount{
int maxIncludingCurrent;
int maxExcludingCurrent;
}
}``````

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

``````public class DiamondCollectionPuzzle {
private static int[] input= {1, 2, 3, 4, 5, 6, 7};
public static void main(String[] args) {
PosssibleDiamondCount possibleDiamonds = collectDiamonds(0);
System.out.println(Math.max(possibleDiamonds.maxExcludingCurrent, possibleDiamonds.maxIncludingCurrent));
}
private static PosssibleDiamondCount collectDiamonds(int startIndex) {
if(startIndex == input.length-1){
PosssibleDiamondCount possibleDiamondCount = new PosssibleDiamondCount();
possibleDiamondCount.maxIncludingCurrent = input[startIndex];
possibleDiamondCount.maxExcludingCurrent = 0;
return possibleDiamondCount;
}else{
PosssibleDiamondCount nextPossibleDiamondCount = collectDiamonds(startIndex + 1);
PosssibleDiamondCount currPossibleDiamondCount = new PosssibleDiamondCount();
currPossibleDiamondCount.maxIncludingCurrent = input[startIndex] + nextPossibleDiamondCount.maxExcludingCurrent;
currPossibleDiamondCount.maxExcludingCurrent = nextPossibleDiamondCount.maxIncludingCurrent;
return currPossibleDiamondCount;
}
}

/**
* Data structure to hold possible diamonds including current and excluding current
*/
private static class PosssibleDiamondCount{
int maxIncludingCurrent;
int maxExcludingCurrent;
}
}``````

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

basically the problem is to find the maximum sum of the array such that no two elements are consecutive.

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

import java.util.Scanner;
class MaxProblem
{
public static void main(String[] args)
{
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int arr[] =new int[n];
for(int i=0;i<n;i++)
arr[i]=scn.nextInt();
System.out.println( FindMaxSum(arr, n));
}
public static int FindMaxSum(int arr[], int n)
{
int incl = arr[0];
int excl = 0;
int excl_new;
for (int i = 1; i < n; i++)
{
excl_new = (incl > excl)? incl: excl;
incl = excl + arr[i];
excl = excl_new;
}
return ((incl > excl)? incl : excl);
}
}

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

import java.util.Scanner;
class MaxProblem
{
public static void main(String[] args)
{
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int arr[] =new int[n];
for(int i=0;i<n;i++)
arr[i]=scn.nextInt();
System.out.println( FindMaxSum(arr, n));
}
public static int FindMaxSum(int arr[], int n)
{
int incl = arr[0];
int excl = 0;
int excl_new;
for (int i = 1; i < n; i++)
{
excl_new = (incl > excl)? incl: excl;
incl = excl + arr[i];
excl = excl_new;
}
return ((incl > excl)? incl : excl);
}
}

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

``````public class MyClass {
public static void main(String args[]) {
int n[]=new int[]{5, 5, 10, 100, 10, 5};
System.out.println(helper(n));
}
public static int helper(int[]n)
{

int dp[]=new int[n.length];
if(n.length==1)
{
return n[n.length-1];
}
if(n.length==2)
{
return n[n.length-2];
}
if(n.length==3)
{
return Math.max(n[n.length-3]+n[n.length-1],n[n.length-2]);
}
dp[n.length-1]=n[n.length-1];
dp[n.length-2]=n[n.length-2];
dp[n.length-3]=n[n.length-3]+n[n.length-1];
for(int i=n.length-4;i>=0;i--)
{
dp[i]=n[i]+Math.max(dp[i+2],dp[i+3]);

}
int result=0;
for(int i=0;i<dp.length;i++)
{
result=Math.max(dp[i],result);
}
return result;
}
}``````

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

``a``

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.