HCL Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

public static int intReverse(int n, int sum) {
		if (n == 0)
			return sum;
		else
			return intReverse(n / 10, sum * 10 + n % 10);
	}
public static void main(String[] args) {
		System.out.println(intReverse(12345, 0));
           }
}

- Anonymous April 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Pushing values onto the stack is worse than using a temporary variable, since temp a single temp variable is O(1), where as your solution requires O(log10 n) on the stack. Not sure an interviewer would allow this.

- jay April 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Idea:

let n = 123456

Note that:
- There are ceil(log10 n) = 6 digits in '123456'
- floor(n / pow(10, ceil(log10 n) - 1)) = 1 (i.e. 123456 / 10000 = 1.23456 -> 1)
- n % 10 (i.e. 123456 % 10 = 6)

Putting this all together (Python):

int(( math.pow(10, 6-1) * (n % 10) + n % math.pow(10, 6-1) ) - (n % 10) + math.floor(n / math.pow(10, 6-1) )) = 623451

Rinse and repeat for the rest of the digits, and you should be able to flip it in place.

(I sure hope there's a better way than this!)

- jay April 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hmm without using temp it would definitely have to be recursive way
anyway here the solution with temp variables (in python)
>>> sum = 0
>>> num = 123
>>> while num:
elem = num % 10
sum = sum*10 + elem
num = num // 10

unfortunately even with recursion i had to use a extra variable .. wonder if that s allowed

>>> def reverse(num,sum):
if num == 0 :
return sum
return reverse(num//10,sum*10+num%10)

>>> reverse(123,0)
>>> 321

- Sandy April 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class IntegerReverse
{
public static void main(String args[])
{
int n = 123456;
rev(n);
}
static void rev(int n)
{
while( n != 0)
{
System.out.print(n%10);
n = n/10;
}
}
}

- Prashanth April 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void test() {
		int n1 = 12332;
		int n2 = 22333;
		
		
		n1 ^= n2;
		n2 ^= n1;
		n1 ^= n2;
	}

- Pouet April 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if int a=10,b=20;

b=a+b;
a=b-a;//a become 20
b=b-a;//b become 10

- jagas August 03, 2013 | 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