LendingKart Interview Question for SDE-2s


Country: India




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

Hi Acharya,

This is to calculate the next number, just like after 1 comes 2, after 2 comes 3 and so on.
Only difference is that you need to increase the middle element first to find the next number i.e. for 455554 next number is 544445.
So we started from middle and went till we got a number which is 4, and we convert it to 5 and again go till middle and set everything to 4. i.e. 455554->555555->544445
(Now to make sense of it in our decimal number system.
9899->9999->9900)
What about the case when we have all 5 then we need to add 4 to beginning and end and keep everything else as 4 i.e. 5555->455554->444444
(Similar to our decimal system 9999->19999->10000)
Below is the code:
The complexity is n*(time taken to generate the next item, which is proportional to the string length)
( 2 + 2 + 4 + 4 + 4 + 4 + 8 + 8 + 8 + 8.. + (n / log(n))) n times
let's convert every string length to n so this will be proportional to O(n^2)

/******************************************************************************

                            Online Java Compiler.
                Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/

public class Main
{
    private static String find(String str, int n){
        StringBuilder sb = new StringBuilder(str);
        while(--n > 0){
            int mid, pos;
            mid = pos = sb.length() / 2;
            //go out till you find a 4 and update to 5
            while(pos < sb.length() && sb.charAt(pos) != '4'){
                pos++;
            }
            if(pos < sb.length()){
                sb.setCharAt(pos, '5');
                sb.setCharAt(mid - (pos - mid) - 1, '5');
            }
            //if no such 4 found means we need to add 4 to beginning and end and reset all to '4'
            else{
                for(int i = 0; i < sb.length(); i++){
                    sb.setCharAt(i, '4');
                }
                sb = new StringBuilder("4"+sb.toString()+"4");
                continue;
            }
            //go in till you find mid and convert everything to 4 
            pos--;
            while(pos >= mid){
                sb.setCharAt(pos, '4');
                sb.setCharAt(mid - (pos - mid) - 1, '4');
                pos--;
            }
        }
        return sb.toString();
    }
	public static void main(String[] args) {
		for(int i = 1; i <40; i++){
		    System.out.println(find("44", i));
		    //System.out.println(Integer.toBinaryString(i));
		}
	}
}

Regards,
Shubham

- yesshubham August 21, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BeautifulNumberWith4And5 {

  private static String getBeautifulNumber(int n) {
    String s = "";
    LinkedList<String> l = new LinkedList<>();
    l.add("");
    while (true) {
      String i = l.remove();
      l.add(i + "4");
      n--;
      if (n == 0) {
        return i + "4" + new StringBuilder(i + "4").reverse();
      }
      l.add(i + "5");
      n--;
      if (n == 0) {
        return i + "5" + new StringBuilder(i + "5").reverse();
      }
    }
  }

  public static void main(String[] args) {
    System.out.println(getBeautifulNumber(1));
    System.out.println(getBeautifulNumber(2));
    System.out.println(getBeautifulNumber(3));
    System.out.println(getBeautifulNumber(4));
  }

}

Generate n the number using 4,5 now concatenate the number of it's reverse.

- xyz September 17, 2019 | 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