## Accenture Interview Question for Data Scientists

Country: United States
Interview Type: Phone Interview

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

``````from random import randint

# Two slightly different answers. The first uses the fact that if i is not next to last and
#  the digits at positions i and  (i+1) are d[i| and d[i+1]
#  and they are such that d[i] < d[i+1] then for sure the minimu is not achieved by removing d[i]

#first method
def get_min(input) :
digits_array = [int(x) for x in str(input)]
minimum = input
for i in range(0,len(str(input))-1) :
if digits_array[i] >= digits_array[i+1] :
d = list(digits_array)
del d[i+1]
minimum = min(minimum, int(''.join(map(str, d))))
del digits_array[len(str(input))-2]
minimum = min(minimum,int(''.join(map(str, digits_array))))
return minimum

# second method finds minimum by checking every possible substitution
def get_min_brute_force(input) :
minimum = input
for i in range(0,len(str(input))-1) :
minimum = min(minimum,step_brut(input, i))
return minimum

def step_brut(number, i) :
d =  [int(x) for x in str(number)]
if d[i] > d[i+1] :
del d[i + 1]
return int(''.join(map(str, d)))
else :
del d[i]
return int(''.join(map(str, d)))

#test with random integers
for i in range(0,10):
n = randint(10000, 1000000)
print(n)
if get_min_brute_force(n) < get_min(n) :
print(' get_min_brute_force= %s ' % get_min_brute_force(n))
print('get_min= &s' %  get_min(n))
else :
print('ok')
print(' get_min_brute_force= get_min = %s ' % get_min_brute_force(n))``````

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

``````import java.io.BufferedReader;
import java.io.InputStream;
public class IntegerX
{
String s;
public IntegerX()throws Exception
{
System.out.print("Enter an integer:");
}
public void get()
{
int n=s.length(),min=Integer.parseInt(s);
for(int i=0,j;i<n-1;i++)
if(i<n-2 && min>(j=Integer.parseInt(s.substring(0,i)+String.valueOf(Integer.parseInt(s.substring(i,i+1))>Integer.parseInt(s.substring(i+1,i+2))?Integer.parseInt(s.substring(i,i+1)):Integer.parseInt(s.substring(i+1,i+2)))+s.substring(i+2,n))))
min=j;
else if(i==n-2 && min>(j=Integer.parseInt(s.substring(0,i)+String.valueOf(Integer.parseInt(s.substring(i,i+1))>Integer.parseInt(s.substring(i+1,i+2))?Integer.parseInt(s.substring(i,i+1)):Integer.parseInt(s.substring(i+1,i+2))))))
min=j;
System.out.println(min);
}
public static void Main(String a[])throws Exception
{
IntegerX psi=new IntegerX();
psi.get();
}
}``````

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

``````def generate(num):
res, s = [], list(int(x) for x in str(num))
for i in range(0, len(s) - 1):
left  = s[:i]
right = s[i+2:]
res  += "".join(str(x) for x in left + [max(s[i], s[i+1])] + right),

# I got max 10 digits, so I run maximum 10 times the loop. Each loop takes O(n) time,
# Where n is the length (10). So at the end I have in the worst case a list of 10 elements each one having 10 digits. Getting the minimum will require me 10 comparisons each one taking 10 (I gotta compare every digit).

return min(res)
num = 233614
print(generate(num))``````

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

``````public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 31299;
String x1 = String.valueOf(x);
char c1 = x1.charAt(0);
char c2 = x1.charAt(1);
StringBuilder sb = new StringBuilder();
for(int i=0;i<x1.length()-1;i++){
c1 = x1.charAt(i);
c2 = x1.charAt(i+1);
if(Integer.valueOf(c1)<=Integer.valueOf(c2)) {
if(i+1 == x1.length()-1) {
String s1 = x1.substring(0, i);
sb.append(s1);
sb.append(Integer.valueOf(c1) > Integer.valueOf(c2) ? c1 : c2);
}
continue;
}
else {
if(i+2 < x1.length()) {
char c3 = x1.charAt(i+2);
if(Integer.valueOf(c2) > Integer.valueOf(c3)) {
String s1 = x1.substring(0, i+1);
sb.append(s1);
//						sb.append(c3);
sb.append(x1.substring(i+2));
break;
}
} else if(i+1 == x1.length()-1) {
String s1 = x1.substring(0, i);
sb.append(s1);
sb.append(Integer.valueOf(c1) > Integer.valueOf(c2) ? c1 : c2);
}
}
}
System.out.println(sb.toString());``````

}

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

``````public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 31299;
String x1 = String.valueOf(x);
char c1 = x1.charAt(0);
char c2 = x1.charAt(1);
StringBuilder sb = new StringBuilder();
for(int i=0;i<x1.length()-1;i++){
c1 = x1.charAt(i);
c2 = x1.charAt(i+1);
if(Integer.valueOf(c1)<=Integer.valueOf(c2)) {
if(i+1 == x1.length()-1) {
String s1 = x1.substring(0, i);
sb.append(s1);
sb.append(Integer.valueOf(c1) > Integer.valueOf(c2) ? c1 : c2);
}
continue;
}
else {
if(i+2 < x1.length()) {
char c3 = x1.charAt(i+2);
if(Integer.valueOf(c2) > Integer.valueOf(c3)) {
String s1 = x1.substring(0, i+1);
sb.append(s1);
//						sb.append(c3);
sb.append(x1.substring(i+2));
break;
}
} else if(i+1 == x1.length()-1) {
String s1 = x1.substring(0, i);
sb.append(s1);
sb.append(Integer.valueOf(c1) > Integer.valueOf(c2) ? c1 : c2);
}
}
}
System.out.println(sb.toString());``````

}

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

``````S is the string of numbers
best = what you get when you replace first two digits with highest
for i in xrange(0, len(S) - 1):
x = what you get when replace i and i+1th digit with highest
if (x < best): best = x

return best``````

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

``````def solution(x):
numberofpairs=[0]*(len(x)-1)
for i in range(len(x)-1):
a=x[i]
b=x[i+1]
if a<b: c = b
else: c=a
numberofpairs[i]=''+x[:i]+c+x[i+2:]
return min(numberofpairs)``````

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

@ricardolira48: Your method fails for every case where n >= 3 and the n-2nd and n-1st digits are greater than the nth digit.

@Dinesh Pant: It's not possible to remove the 6 because both of its adjacent numbers are smaller. Remember, for any adjacent pair, you remove the smaller and retain the larger of the two.

Brute method
Swift 2.2

``````extension Int {
var array: [Int] {
var number = self
var result = [Int]()
while (number != 0) {
result.insert(number % 10, atIndex: 0)
number = number / 10

}
return result

}

}

extension Array {
var int: Int {
var result = 0
var multiplier = 1
for i in self.reverse() {
if let digit = i as? Int {
result += digit * multiplier
multiplier *= 10

}

}
return result

}

}

func smallestSwap(num: Int) -> Int {
var min = Int.max
for i in 1 ..< num.array.count {
var numArray = num.array
if (numArray[i - 1] < numArray[i]) {
numArray.removeAtIndex(i - 1)

} else {
numArray.removeAtIndex(i)

}
let newNum = numArray.int
if (newNum < min) {
min = newNum

}

}
return min

}

smallestSwap(233614)``````

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

I feel, example is wrong. x=233614 should return 23314.

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

replace last 2 digits

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.