Amazon Interview Question for Java Developers


Country: United States




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

import java.util.stream.IntStream;

/**
 * @author mahesh.vira
 *
 */
public class UnLuckyNumber4 {

  /**
   * @param args
   */
  public static void main(String[] args) {

    int n = 20;
    
    long count = IntStream.rangeClosed(1, n).filter(UnLuckyNumber4::isHavingNbr4).count();
    
    System.out.println(n-count);
  }
  
  private static boolean isHavingNbr4(int nbr){
    while(nbr>0){
      int r = nbr%10;
      nbr = nbr /10;
      if(r==4){
        return true;
      }
    }
    return false;
  }

}

- learn.vira.mahesh May 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Will work but I like Bob L 's python answer better with O(lg(number)) complexity.

- ofekpearl November 17, 2018 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Here's a python solution

def remove4(number):
    if number == 0:
        return 0
    
   
    digit = int( math.log10(number) ) 
    theNumber = int(int(number) / int(10 ** digit))
    theRest = number - theNumber * 10 ** digit
    
    if theNumber >= 4:
        theNumber -= 1
    
    return theNumber * 9 ** digit + remove4(theRest)

- Bob L April 30, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Perfect, I like this, O(lg(number)) complexity.

- ofekpearl November 17, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

remove4(399-499) should all be 323. Above code answers 243-323.

- paul May 01, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can it be simple?

public int remove4(int n) {
		int fours = 0;
		for(int i=4; i<=n; i+=4) {
			fours++;
		}
		return n - fours;
	}

- Santhosh May 01, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int countLevels(int n, int unluckyFloor){
int countLevels = 0;
for(int level = 1; level<=n; level++){
if(level % 10 != unluckyFloor){
countLevels ++;
}
}
return countLevels;
}

- Heta Jhaveri May 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function findFloors(n, unlucky) {
    var count = 0;
    while(n > 0) {
        if(n % 10 !== unlucky) {
            count++;
        }
        n--;
    }
    return count;
};

- shashi.dokania July 12, 2018 | 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