FreshoKartz Interview Question for Software Engineer Interns


Country: India
Interview Type: Phone Interview




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

def tidynumber(number):
line=str(number)
prev_number="0"
for i in xrange(len(line)-1):
if line[i]<prev_number:
left=str(int(line[:i])-1)
right="9"*(len(line)-i)
return left+right
prev_number=line[i]
return number
n=input()
print tidynumber(n)

- batman_2702 April 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int greatestSmallerTidyNumber(int n) {
		char[] chars = String.valueOf(n).toCharArray();
		boolean foundbig = false;
		int lastEqualIndex = -1;
		for (int i = 0; i < chars.length - 1; i++) {
			if (foundbig) {
				chars[i] = '9';
				continue;
			}
			if (chars[i] == chars[i + 1]) {
				lastEqualIndex = i;
				continue;
			}
			if (chars[i] > chars[i + 1]) {
				if (lastEqualIndex != -1 && lastEqualIndex == i - 1) {
					--chars[i - 1];
				}
				if (chars[i] == '1') {
					chars[i] = '9';
				} else {
					--chars[i];
				}
				foundbig = true;
			}
		}
		if (foundbig) {
			chars[chars.length - 1] = '9';
		}
		if (Integer.valueOf(new String(chars).trim()) > n) {
			chars[chars.length - 1] = ' ';
		}
		return Integer.valueOf(new String(chars).trim());
	}

- rajendra April 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is from the google code jam this year lol

- trevorvanloon April 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@rajendra : I actually gave you an up vote. Later, I found, almost there but not there.
Observe this :
Input : 332 Expected : 299
Actual : 229
I was looking for branch conditions. Gotha.
I guess the dumb approach wins here :-)

def find_unoptimal( n ){ 
   while( !is_tidy(n) ){ n -= 1 }
   n // return   
}

Obviously I will try to improve, but there are some very interesting edge scenarios.

- NoOne April 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am hungry, SRH lost so frustrated, but this should do the trick :

/* 
The find next lower tidy number is 
not a classically trivial one.
Obvious trivial solution is :

def find_unoptimal( n ){ while( !is_tidy(n) ){ n -= 1 } }

But that is a mess.
A better solution will be:
1. Search for inversion of order ( d[i] > d[i+1] ) from left.
2. If on the inversion, we can down the digit:
   Down(x) = x - 1 
   and d[i-1] < d[i] - 1 then we can down the digit, replace rest by 9. 
3. If we can not do that ( 12222334445555123 ), 
   we search for a digit change from right: ( d[i]<d[i+1] ) 
   where we can change d[i+1] to d[i] and replace all right digits by 9
*/

def find_last_tidy( n ){
  sn = str(n)
  l = size(sn)
  i = index( [0:l-1] ) :: { sn[$.o] > sn[$.o+1] }
  if ( i < 0 ) return n // this is first step 
  // is there a left digit? if not... 
  if ( i == 0 ) return int( '' + ( int(sn[0]) - 1 ) + ( '9' ** (l - 1) ) )
  // there is 
  // if it is like 12222334445555|123 ?
  // then we need to check j is the repeat size 
  j = index( [i:-1] ) :: { sn[$.o] != sn[i] } 
  ns = sn[0:i-j] + ( int(sn[i]) - 1 ) + ( '9' ** (l - i + j - 2 ) ) 
  int ( ns )
}
println( find_last_tidy(@ARGS[0]) )

- NoOne April 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ {{{int[] input; int[] output; //initialize output to all '9's for(int i = 0; i < intput.Length - 1; i++) { output[i] = 9; } Boolean isSmaller = true; //loop through intput except last dight for(int i = 0; i < intput.Length; i++) { if(i < intput.Length - 1) { if(input[i] < intput[i+1]) { output[i] = input[i]; } else if(input[i] > intput[i+1]) { output[i] = input[i] - 1; isSmaller = false; break; // stop here } else { //equal to next digit to the right output[i] = -1; //to determine later } } } if(isSmaller) //last digit { output[intput.Length - 1] = intput[intput.Length - 1]; } //fix to-determine-later digits by copying from its right for(int i = Length - 2; i >= 0; i++) { if(output[i] == -1) { output[i] = output[i+1]; } } }}} - Louis April 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@NoOne, here is the improved one which will work for all edge cases.

public static int greatestSmallerTidyNumber(int n) {
		int k = -1;
		char[] chars = String.valueOf(n).toCharArray();
		boolean foundbig = false;
		for (int i = 0; i < chars.length - 1; i++) {
			if (foundbig) {
				chars[i] = '9';
				continue;
			}
			if (chars[i] > chars[i + 1]) {
				k = i;
				while (--k >= 0) {
					if (chars[k] != chars[i]) {
						break;
					}
				}
				i = k + 1;
				if (chars[i] == '1') {
					chars[i] = '9';
				} else {
					--chars[i];
				}
				foundbig = true;
			}
		}
		if (foundbig) {
			chars[chars.length - 1] = '9';
		}
		if (Integer.valueOf(new String(chars).trim()) > n) {
			chars[chars.length - 1] = ' ';
		}
		return Integer.valueOf(new String(chars).trim());
	}

- rajendra April 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int greatestSmallerTidyNumber(int n) {
		int k = -1;
		char[] chars = String.valueOf(n).toCharArray();
		boolean foundbig = false;
		for (int i = 0; i < chars.length - 1; i++) {
			if (foundbig) {
				chars[i] = '9';
				continue;
			}
			if (chars[i] > chars[i + 1]) {
				k = i;
				while (--k >= 0) {
					if (chars[k] != chars[i]) {
						break;
					}
				}
				i = k + 1;
				if (chars[i] == '1') {
					chars[i] = '9';
				} else {
					--chars[i];
				}
				foundbig = true;
			}
		}
		if (foundbig) {
			chars[chars.length - 1] = '9';
		}
		if (Integer.valueOf(new String(chars).trim()) > n) {
			chars[chars.length - 1] = ' ';
		}
		return Integer.valueOf(new String(chars).trim());
	}

- rajendra April 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function isTidy(n) {
  var s = n.toString();
  var prev = 0;
  for(var i = 0; i < s.length; i++) {
    if (s[i] < prev) {            
      return false;            
      break;
    }      
    else {
      prev = s[i];
    }
  }
  return true;
}


let input = 143456;

if (isTidy(input)) {
  console.log(input)
} 
else {
  for(var i = input; i > 0; i--) {
    if (isTidy(i)) {
      console.log(i);
      break;
    }
  }    
}

- Vladimir April 24, 2017 | 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