Accenture Interview Question
Data ScientistsCountry: United States
Interview Type: Phone Interview
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class IntegerX
{
String s;
public IntegerX()throws Exception
{
System.out.print("Enter an integer:");
s=new BufferedReader(new InputStreamReader(System.in)).readLine();
}
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();
}
}
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))
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());
}
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());
}
@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)
- Luke Rhinehart August 02, 2016