Software engineer intern Interview Questions
software engineer intern interview questions shared by candidates
Top Interview Questions
A task in which I was required to print all numbers between 1 and a million - with no repeating digits - Easy enough! Just can throw you off guard a bit public static void noRepeat(int n) { int[] ar; for(int i=1;i1) { flag = 1; } temp = temp/10; } if(flag==0) { System.out.println(i); } } } Your answer is correct, but it is slow. Here you are an algorithm that does same thinkg as you do, but also one more algorithm - using permutations. Instead of outputting to System.out I simply calculate how many numbers are there. Run and see how slow is your method for n = 8 (where n is the number of digits). import java.io.*; import java.util.*; public class NumbersNoRepeatDigits{ public class StopWatch { private long startTime = 0; private long stopTime = 0; private boolean running = false; public void start() { this.startTime = System.currentTimeMillis(); this.running = true; } public void stop() { this.stopTime = System.currentTimeMillis(); this.running = false; } //elaspsed time in milliseconds public long getElapsedTime() { long elapsed; if (running) { elapsed = (System.currentTimeMillis() - startTime); } else { elapsed = (stopTime - startTime); } return elapsed; } //elaspsed time in seconds public long getElapsedTimeSecs() { long elapsed; if (running) { elapsed = ((System.currentTimeMillis() - startTime) / 1000); } else { elapsed = ((stopTime - startTime) / 1000); } return elapsed; } } // mode complicated solution public static void permuteArray(int[] a){ permuteArrayCore(a, a.length - 1); } public static void permuteArrayCore(int[] a, int i){ if (i == -1) { if (a[0] > 0) printArray(a); return; } permuteArrayCore(a, i-1); // when ith element is on place; for (int j=i-1; j>=0; j--) { //swap i and j swap(a, i, j); permuteArrayCore(a, i-1); swap(a, i, j); } } private static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void selectMAmong10(int m) { boolean[] take = new boolean[10]; selectMAmongNCore(m, 0, take, 0); } public static void selectMAmongNCore(int m, int next, boolean[] take, int countTaken) { if (countTaken == m){ int[] x = new int[m]; int xi = 0; for (int i = 0; i = m - countTaken) selectMAmongNCore(m, next + 1, take, countTaken); } // simpliest solution public static boolean checkDistinct(int i) { boolean[] digits = new boolean[10]; while (i > 0) { int rem = i % 10; if (digits[rem]) return false; digits[rem] = true; i = i / 10; } return true; } public static int totalCount = 0; public static void printArray(int[] a){ totalCount++; /* for (int i = 0; i < a.length; ++i) System.out.format("%d ", a[i]); System.out.println(); */ } public static void main(String[] args){ int n = 8; NumbersNoRepeatDigits t = new NumbersNoRepeatDigits(); NumbersNoRepeatDigits.StopWatch st = t.new StopWatch(); st.start(); for (int i = 1; i <= n; ++i) selectMAmong10(i); System.out.println(totalCount); System.out.println(st.getElapsedTime()); totalCount = 0; System.out.println("-----------"); int max = (int)Math.pow(10, n); for (int i = 1; i < max ; ++i) { if (checkDistinct(i)) // System.out.println(i); printArray(null); } System.out.println(totalCount); st.stop(); System.out.println(st.getElapsedTime()); } } looks really simple (if I understood it right). Here is the SHORT java version. static boolean hasDupDigit(int i){ boolean[] digits = new boolean[10]; while(i!=0){ int r = i%10; if(digits[r])return true; digits[r] = true; i/=10; } return false; } static void printNums(){ for (int i=1;i<=1000000;i++) if(!hasDupDigit(i)) System.out.println(i); } Show more responses One or more comments have been removed. |
How does Java Garbage Collection work? How would you implement garbage collection? |
Given an array of integers, provide an efficient way of finding an integer that appears only once in this array. |
Would Java Bytecode generated by Java 7 work on a JVM made to function on Java 1.0? |
Classic one from them: You have an array and you want to find the first non-repeating element. |
How would you delete/add an element from a linked list? |
What do you do when your hashtable becomes exhausted? |
How to calculate the square root of a number? |
You have a linked list. How would you detect a loop in it, without using DFS of BFS? |
How would you gather input from users (name, dob, address) and reprocate the same as output? |
See Interview Questions for Similar Jobs
- Software Engineer
- Intern
- Software Developer
- Technology Analyst
- Summer Analyst
- Senior Software Engineer
- Analyst
- Graduate Software Engineer
- Software Development Engineer
- Senior Software Developer