interview-questions

Interview Question - How to programmatically screw in a lightbulb

Below is an interview question I've been pondering using to test an interviewee's critical thinking and creativity skills. Leave the interviewee alone for 5-10 minutes and have him/her answer the following question: How would you programmatically screw in a lightbulb? Answers I would be hoping to see would be similar to: bool FixLigh...

Interview Question: What is a hashmap?

I was asked this in an interview: "Tell me everything you know about hashmaps." I proceeded to do just that: it's a data structure with key-value pairs; a hash function is used to locate the element; how hash collisions can be resolved, etc. After I was done, they asked: "OK, now explain everything you just said to a 5-year-old. You c...

Puzzle : finding out repeated element in an Array

Size of an array is n.All elements in the array are distinct in the range of [0 , n-1] except two elements.Find out repeated element without using extra temporary array with constant time complexity. I tried with o(n) like this. a[]={1,0,0,2,3}; b[]={-1,-1,-1,-1,-1}; i=0; int required; while(i<n) { b[a[i]]+...

Finding median of large set of numbers too big to fit into memory

I was asked this question in an interview recently. There are N numbers, too many to fit into memory. They are split across k database tables (unsorted), each of which can fit into memory. Find the median of all the numbers. Wasn't quite sure about the answer to this one. ...

Finding the no of leaf nodes

An N-ary tree has N sub-nodes for each node. If the tree has M non-leaf nodes, How to find the no of leaf nodes? ...

Logical shift right operation on signed integer

Logical shift right by 3 operation on signed integer -28. What's the correct answer? +203 +83 +3 -3 2's complement of -28 is 11100100. Now if I apply logical right shift operation I am not getting any one of above answers. ...

Shuffle array variables in a pre-specified order, without using extra memory of "size of input array"

Input : A[4] = {0,4,-1,1000} - Actual Array P[4] = {1,0,3,2} - Order to be reshuffled Output: A[4] = {4,0,1000,-1} Condition : Don't use an additional array as memory. Can use an extra variable or two. Problem : I have the below program in C++, but this fails for certain inputs of array P. #include<iostream> using nam...

Find cycle of shortest length in a directed graph with positive weights

Hi guys, I was asked this question in an interview but i couldnt come up with any decent solution. So, i told them the naive approach of finding all the cycles then picking the cycle with the least length. I'm curious to know what is an efficient solution to this problem. Thanks, Chander ...

Check if a number is non zero using bitwise operators in C.

Check whether a number x is nonzero using the legal operators except !. Examples: isNonZero(3) = 1, isNonZero(0) = 0 Legal ops: ~ & ^ | + << >> Note : Only bitwise operators should be used. if, else, for, etc. cannot be used. Edit1 : No. of operators should not exceed 10. Edit2 : Consider size of int to be 4 bytes. int isNonZero(...

Check if a number x is positive (x>0) by ONLY using bitwise operators in C.

isPositive - return 1 if x > 0, return 0 otherwise Example: isPositive(-1) = 0. Legal ops: ! ~ & ^ | + << >>(arithmetic shift) Max ops: 8 Note: No conditional statements are allowed. size of int is a word(4 bytes). It is a signed representation using two's compliment. int isPositive(int x) { return ???; } ...

Find the number in an array which occurred most number of times

Hi, Given an integer array, i need to find which number occurred most number of times. I have written algorithm as below. Use a map to store number and number of times it occurred. map<int, int> Key: represents the number value: represents number of times key occurred. Scan input array and update the map with number ...

Algorithm Interview Question

Hi all: I went to an interview today and here is the question: Suppose you have got 1 billion integers which are unsorted at one disk file,please find out the largest 100 numbers,try the best solution as you can. Anybody who has ideas,please do share! Thanks in advance! ...

Another Algorithm Job-Interview Question

Hi all: So here is the question: Suppose you have got 100 thousands integers which ranges from 1 to 1 million,please sort out the integers and its time complexity should be O(n) Anybody who shares his or her ideas is well appreciated. Thanks in advance! ...

Questions every good C/C++ Developer should be able to answer?

I was going through interview questions tag and found those question are quite useful, so in the same spirit, I am asking this question for C and C++ developers What questions do you think good C and C++ programmers should be able to answer? Looking forward for some amazing responses. Please answer questions too, so that people could ...

Why friend function can't be used for overloading assignment operator ?

Hi, Assignment operator can be overloaded using member function but not friend function. Sample code is like below. class Test { int a; public: Test(int x) :a(x) {} friend Test& operator=(Test &obj1, Test &obj2); }; Test& operator=(Test &obj1, Test &obj2)//Not implemented fully. just for test. { return obj1...

Is there any way to write a class such that no class can be inherited from it ?

Hi, From link http://www.coolinterview.com/interview/10842/ Is there any way to write a class such that no class can be inherited from it ? From suggestions in the above link, i tried below code class A { A(){} ~A(){} A(const A&); A& operator=(const A&); }; class B: public A { }; The above code doesn't produce any e...

How can this Java code be improved to find sub-string in a string?

Hi, I was recently asked to submit a solution to a problem for a job. Problem: Find a sub-string in a string. Input: "Little star's deep dish pizza sure is fantastic." Search: "deep dish pizza" Output: "Little star's [[HIGHLIGHT]]deep dish pizza[[ENDHIGHLIGHT]] sure is fantastic." Note that the highlighter doesn't have to have...

seat resv. problem

Reservation Problem there are 67 seats in train . there are only 5 seats in a row and in last row there are only 2 seats. One person can reserve only 5 seat at a time. If person reserving seat , the care is taken that he may get all in row. if seats are not available in row then the arrangement is so that person group get nearby seats. ...

Rearranging array elements

Not sure if it is a duplicate. Given a data structure having first N integers and next N chars. A = i1 i2 i3 ... iN c1 c2 c3 ... cN. I need an in-place algorithm to rearrange the elements as A = i1 c1 i2 c2 ... iN cN. ...

Algorithm for permutations of operators and operands

I came across this question on an interview website - We are given 4 numbers say n1, n2, n3, n4. We can place them in any order and we can use the mathematical operators +, -, *, / in between them to have the final result as 24. Write an algorithm for this - it will take 4 numbers and return false or true whether final result 24 is p...