Software engineer intern Interview Questions | Glassdoor.co.uk

Software engineer intern Interview Questions

44

software engineer intern interview questions shared by candidates

Top Interview Questions

Sort: RelevancePopular Date

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

7 Answers

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); }

One or more comments have been removed.
Please see our Community Guidelines or Terms of Service for more information.

How does Java Garbage Collection work? How would you implement garbage collection?

2 Answers

Given an array of integers, provide an efficient way of finding an integer that appears only once in this array.

2 Answers

Would Java Bytecode generated by Java 7 work on a JVM made to function on Java 1.0?

2 Answers

Classic one from them: You have an array and you want to find the first non-repeating element.

2 Answers

How would you delete/add an element from a linked list?

1 Answer

What do you do when your hashtable becomes exhausted?

1 Answer

How to calculate the square root of a number?

1 Answer

You have a linked list. How would you detect a loop in it, without using DFS of BFS?

1 Answer

How would you gather input from users (name, dob, address) and reprocate the same as output?

1 Answer
110 of 44 Interview Questions