## StartUp Interview Question for Java Developers

Country: India
Interview Type: Written Test

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

``````/*
Observe that the problem is that of finding locations where
we can cut the string.
Given a string of length 's' has exact s-1 locations where we can cut it
That s-1 becomes the number of choices = n.
Clearly then the number of comma = k is the cut locations to be selected.
It is obvious that the order in which the cut was imposed does not matter.
Thus, the choices are C(n,k).
Thus, the problem is:
1. finding combinations of cut
2. Applying the cuts
3. Maintaining a max out of the cuts
*/

def do_fancy(string, k){
n = size(string) - 1
range = [0:k+1]
max = 0
for ( t : comb(range,k) ){
start_inx = 0
words = list()
for ( i = 0; i < k ; i+= 1 ){
words += int( string[start_inx:t[i]] )
start_inx = t[i] + 1
}
words += int( string[start_inx:-1] )
#(m,M) = minmax( words )
#(m,max) = minmax( max, M )
}
max // return
}

println( do_fancy( '89769957',3 ) )``````

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

Code in Python. Works for all above inputs.

``````#s = '999'
#k = 1
#output = 9,99 or 99,9 : 99
#s = '999'
#k = 2
#output = 9,9,9 : 9
s = '857'
k = 1
#output = 8,57 or 85,7 : 85

def check(s,k):
len1 = len(s)
if len1 <= k:
return None

#pointer = len1/(k+1)
#print pointer

list1 = []
s1 = ''
s2 = ''
for i in range (0,k):
list1.append(s[i])

for i in range (k,len1):
s1 = s1 + s[i]

list1.append(s1)

for i in range(len1-1,len1-1-k,-1):
list1.append(s[i])

for i in range(0,len1-k):
s2 = s2+s[i]

list1.append(s2)

temp = 0

for items in list1:
intitems = int(items)
if intitems > temp:
temp = intitems

return temp

highest = check(s,k)
print highest``````

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

#!usr/bin/env python
import sys

def listStrings(string, numofcommas):
i = numofcommas
while i < len(string):
string = string[:i] + ',' + string[i:]
i = i + 1
i = i + numofcommas
print(string)
string_list.append(string)
return string_list

def preCheck(string, numofcommas):
if len(string) <= numofcommas:
print "Given String ' {} ' length is less than that of numberof commas hence can't be splitted".format(string)
sys.exit()
else:
listStrings(string,numofcommas)
rev_str = string[::-1]
listStrings(rev_str,numofcommas)

def checkgreatest(str_list):
greatest_num = 0
for i in str_list:
#print i
#print type(i)
mini_list = i.split(',')
#print mini_list
if max(mini_list) > greatest_num:
greatest_num = max(mini_list)
print "{} is the highest Number among the {} list".format(greatest_num,str_list)
return greatest_num

if __name__ == '__main__':
string_list = []
#string = raw_input("Enter the numeric string")
str_list = preCheck("12345674",1)
checkgreatest(string_list)

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

package com.example.collection;

public class InsertComma {
public static void main(String[] args) {
StringBuilder inputString = new StringBuilder("85739");
int k = 2;
int noOfWaysToAddComma = inputString.length() - k;
int max = 0;
int index = 0;

StringBuilder newInputString = new StringBuilder("85739");
insertComma(newInputString, k, index);
String myString = newInputString.toString();
char [] charArray = myString.toCharArray();
String [] arr = myString.split(",");
int newMax = getMaximumInStringArray(arr);
max = newMax > max ? newMax : max;
index ++;
}
System.out.println(max);
}
private static void insertComma(StringBuilder newInputString, int k, int index) {
int n = newInputString.length();
for(int j=1, i = 0; j < (n+k) && i < k ; j += k) {
newInputString.insert(j+index, ',');
i++;
}
System.out.println(newInputString);
}
private static int getMaximumInStringArray(String[] arr) {

int max = arr[0].isEmpty() ? 0 : Integer.parseInt(arr[0]);
for(int i = 1 ; i < arr.length ; i++) {
max = !arr[i].isEmpty() ?( Integer.parseInt(arr[i]) > max ? Integer.parseInt(arr[i]) : max) : 0;
}
return max;
}
}

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

public class InsertComma {
public static void main(String[] args) {
StringBuilder inputString = new StringBuilder("85739");
int k = 2;
int noOfWaysToAddComma = inputString.length() - k;
int max = 0;
int index = 0;

StringBuilder newInputString = new StringBuilder("85739");
insertComma(newInputString, k, index);
String myString = newInputString.toString();
char [] charArray = myString.toCharArray();
String [] arr = myString.split(",");
int newMax = getMaximumInStringArray(arr);
max = newMax > max ? newMax : max;
index ++;
}
System.out.println(max);
}
private static void insertComma(StringBuilder newInputString, int k, int index) {
int n = newInputString.length();
for(int j=1, i = 0; j < (n+k) && i < k ; j += k) {
newInputString.insert(j+index, ',');
i++;
}
System.out.println(newInputString);
}
private static int getMaximumInStringArray(String[] arr) {

int max = arr[0].isEmpty() ? 0 : Integer.parseInt(arr[0]);
for(int i = 1 ; i < arr.length ; i++) {
max = !arr[i].isEmpty() ?( Integer.parseInt(arr[i]) > max ? Integer.parseInt(arr[i]) : max) : 0;
}return max;}}

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

``````public class InsertComma {
public static void main(String[] args) {
StringBuilder inputString = new StringBuilder("85739");
int k = 2;
int noOfWaysToAddComma = inputString.length() - k;
int max = 0;
int index = 0;

StringBuilder newInputString = new StringBuilder("85739");
insertComma(newInputString, k, index);
String myString = newInputString.toString();
char [] charArray = myString.toCharArray();
String [] arr = myString.split(",");
int newMax = getMaximumInStringArray(arr);
max = newMax > max ? newMax : max;
index ++;
}
System.out.println(max);
}
private static void insertComma(StringBuilder newInputString, int k, int index) {
int n = newInputString.length();
for(int j=1, i = 0; j < (n+k) && i < k ; j += k) {
newInputString.insert(j+index, ',');
i++;
}
System.out.println(newInputString);
}
private static int getMaximumInStringArray(String[] arr) {

int max = arr[0].isEmpty() ? 0 : Integer.parseInt(arr[0]);
for(int i = 1 ; i < arr.length ; i++) {
max = !arr[i].isEmpty() ?( Integer.parseInt(arr[i]) > max ? Integer.parseInt(arr[i]) : max) : 0;
}
return max;
}
}``````

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

Inserting commas is pointless if we need to return the number anyways. The simple solution is to iterate through the array, call it String s, the commas denote the largest int, obviously it will be made if the commas are placed consecutively after one another, in a five digit 2 comma example 85788, having two non consecutive commas: 8,57,88 makes no sense as it will shorten the number of digits. As such just iterate through the array and find the number that fits number of digits minus number of commas.

{{

def comma_sep(String s, int k):
max = 0
num_digits=len(s)-k
if len(s)-k<0:
return None
else:
for i in range(len(s)-1-num_digits):
temp = int(s[i: i+num_digits-1])
if max < temp:
max = temp

return temp
}}}

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

Language - Java
private static int InsertComma(String input, int i) {
StringBuilder sb = new StringBuilder();
int j = 0;
int size = input.length();
while (j < size) {
StringBuilder tmp = new StringBuilder();
for (int j2 = 0; j2 < i; j2++) {
if (j < size) {
tmp.append(input.charAt(j));
}
j++;
}
sb.append(tmp);
if (j < size) {
sb.append(",");
}
}
String []st=sb.toString().split(",");
int max=0;
for (int i1 = 0; i1 < st.length; i1++) {
int tmp=Integer.parseInt(st[i1]);
if(tmp>max){
max=tmp;
}
}
return max;
}

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

//\$Id\$

public class InsertCommas {

public static void main(String[] args) {
int noOfCommas = 5;
String number = "456809";
int max = 0;
int j;

for(int i=0;i<=number.length()-noOfCommas;i++){
int index = i;
if(index >0 && max < Integer.parseInt(number.substring(0,index))){
max = Integer.parseInt(number.substring(0,index));
}
for(j=0;j<noOfCommas;j++){
if(max < Integer.parseInt(number.substring(index,index+1))){
max = Integer.parseInt(number.substring(index,index+1));
}
index+=1;
}
if( index < number.length()-1 && max < Integer.parseInt(number.substring(index,number.length()))){
max = Integer.parseInt(number.substring(index,number.length()));
}
}

System.out.println(max);

}

}

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

``````//\$Id\$

public class InsertCommas {

public static void main(String[] args) {
int noOfCommas = 5;
String number = "456809";
int max = 0;
int j;

for(int i=0;i<=number.length()-noOfCommas;i++){
int index = i;
if(index >0 && max < Integer.parseInt(number.substring(0,index))){
max = Integer.parseInt(number.substring(0,index));
}
for(j=0;j<noOfCommas;j++){
if(max < Integer.parseInt(number.substring(index,index+1))){
max = Integer.parseInt(number.substring(index,index+1));
}
index+=1;
}
if( index < number.length()-1 && max < Integer.parseInt(number.substring(index,number.length()))){
max = Integer.parseInt(number.substring(index,number.length()));
}
}

System.out.println(max);

}

}``````

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

public class InsertCommas {

public static void main(String[] args) {

StringBuilder inputString = new StringBuilder("85739");
int k = 4;
int noOfWaysToAddComma = inputString.length() - k;
int max = 0;

for(int i = 0; i < noOfWaysToAddComma; i++) {

StringBuilder newInputString = new StringBuilder("85739");
insertComma(newInputString, k, i);
String myString = newInputString.toString();
String [] arr = myString.split(",");
int newMax = getMaximumInStringArray(arr);
max = newMax > max ? newMax : max;

}
System.out.println(max);

}

private static void insertComma(StringBuilder newInputString, int k, int index) {

int j = 1;
for(int i=1; i <= k; i++, j+=2) {
newInputString.insert(j+index, ',');
}
System.out.println(newInputString);
}

private static int getMaximumInStringArray(String[] arr) {

int max = arr[0].isEmpty() ? 0 : Integer.parseInt(arr[0]);
for(int i = 1 ; i < arr.length ; i++) {
max = !arr[i].isEmpty() ?( Integer.parseInt(arr[i]) > max ? Integer.parseInt(arr[i]) : max) : 0;
}
return max;
}

}

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

public class InsertCommas {

public static void main(String[] args) {

StringBuilder inputString = new StringBuilder("85739");
int k = 2;
int max = 0;

for(int i = 0; i < inputString.length() - k; i++) {

StringBuilder newInputString = new StringBuilder("85739");
insertComma(newInputString, k, i);
String myString = newInputString.toString();
String [] arr = myString.split(",");
int newMax = getMaximumInStringArray(arr);
max = newMax > max ? newMax : max;

}
System.out.println(max);

}

private static void insertComma(StringBuilder newInputString, int k, int index) {

int j = 1;
for(int i=1; i <= k; i++, j+=2) {
newInputString.insert(j+index, ',');
}
System.out.println(newInputString);
}

private static int getMaximumInStringArray(String[] arr) {

int max = arr[0].isEmpty() ? 0 : Integer.parseInt(arr[0]);
for(int i = 1 ; i < arr.length ; i++) {
max = !arr[i].isEmpty() ?( Integer.parseInt(arr[i]) > max ? Integer.parseInt(arr[i]) : max) : 0;
}
return max;
}

}

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.