Oracle Interview Question for Software Engineer / Developers


Country: United States




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

void intpart(string pref, int n, int maxi){
	if(n==0){
		cout << pref << endl;
		return;
	}
	for(int i=1; i<=maxi && i<=n; i++)
		intpart(pref+(pref==""?"":"+")+to_string(i), n-i,i);
}

int main(){
	int n=10;
	intpart("", n, n);
	return 0;
}

- me February 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is it O(N)

- CIsSharp June 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void all(int t){
	String R[][] = new String[t+1][];
	R[0] =new String[0];
	for(int i = 1; i <= t; i++){
		R[i] = new String[(i/2) + 1];
		for( int j = 1; j <= i/2; j++){
			R[i][j] = (i-j) + "+" + j + "";
		}
	}
	System.out.println(" -> ["+t+"] =  ");
	for(int i = t; i >= 0 ; i-- ){
		for(int j = 1; j < R[i].length; j++){
			System.out.print(R[i][j]);
			for(int x= i; x < t; x++){
				 System.out.print("+1");
			}
			System.out.print(", ");
			
		}
		System.out.println();
	}
	System.out.println();
}

test case:

all(10);

output:

-> [10] =
9+1, 8+2, 7+3, 6+4, 5+5,
8+1+1, 7+2+1, 6+3+1, 5+4+1,
7+1+1+1, 6+2+1+1, 5+3+1+1, 4+4+1+1,
6+1+1+1+1, 5+2+1+1+1, 4+3+1+1+1,
5+1+1+1+1+1, 4+2+1+1+1+1, 3+3+1+1+1+1,
4+1+1+1+1+1+1, 3+2+1+1+1+1+1,
3+1+1+1+1+1+1+1, 2+2+1+1+1+1+1+1,
2+1+1+1+1+1+1+1+1,
1+1+1+1+1+1+1+1+1+1,

Here in the complexity, I need help. It seems that It is n^3 worse case ( but not sure )
Complexity

O ( n* n/2) + O (n*(n/2)*t) = O (n^2) ??? please help.

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> February 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A little improvement on my previous approach. But It still O (n^3)

Is there any better way to improve this ?

public static void printAll(int t){
 		System.out.println(t + " = ");
 		for(int i=2; i <= t; i++){
 			for(int j=1; j <= i/2 ; j++){
 				System.out.print((i-j) + "+" + j);
 				for(int x=i; x < t; x++){
 					System.out.print("+1");
 				}
			System.out.println();	 			
 			}
 		}
 	}

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> February 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

It seems like you're missing cases e.g. 2+2+2+2+2

- tp February 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

#trueStory good catch. Well... Let me think about a new solution. I will get back soon. thx

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> February 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Look up the rod partition problem in CLRS.

- CLRS expert February 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import sys

if len(sys.argv) < 2:
        exit(1)

T = int(sys.argv[1])

def getSum(t):
        l = [[1,1]]
        for i in xrange(3, t+1):
                for j in xrange(len(l)):
                        l[j].append(1)
                for j in xrange(1, int(i/2)+1):
                        l.append([j, i-j])
        return l
for l in getSum(T):
        for n in l:
                print n,
        print ""

It's in o(n^2)

- Abhi February 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PrintNumbers {

static int n = 13;

public static void main(String[] args) {

for (int i = 1; i < n; i++) {
int k = n / i;
StringBuilder sb = new StringBuilder("");
for (int j = 1; j <= k; j++) {
if (j == 1) {
sb.append(i);
} else {
sb.append(" + " + i);
}
}
if (i * k < n) {
display(n - (i * k), sb.toString());
} else {
System.out.println(sb.toString());
}
}
}

public static void display(int n, String str) {

for (int i = 1; i <= n; i++) {
StringBuilder sb = new StringBuilder(str);
int k = n / i;
for (int j = 1; j <= k; j++) {
sb.append(" + " + i);
}
if (i * k < n) {
display(n - (i * k), sb.toString());
} else if (i * k == n) {
System.out.println(sb.toString());
}

}
}
}


Output number T=13

1 + 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+ 1 + 1 + 4 + 6
1 + 2 + 4 + 6
3 + 4 + 6
1 + 1 + 5 + 6
2 + 5 + 6
1 + 6 + 6
1 + 1 + 1 + 1 + 1 + 1 + 7
1 + 1 + 1 + 1 + 2 + 7
1 + 1 + 2 + 2 + 7
2 + 2 + 2 + 7
1 + 1 + 1 + 3 + 7
1 + 2 + 3 + 7
3 + 3 + 7
1 + 1 + 4 + 7
2 + 4 + 7
1 + 5 + 7
6 + 7
1 + 1 + 1 + 1 + 1 + 8
1 + 1 + 1 + 2 + 8
1 + 2 + 2 + 8
1 + 1 + 3 + 8
2 + 3 + 8
1 + 4 + 8
5 + 8
1 + 1 + 1 + 1 + 9
1 + 1 + 2 + 9
2 + 2 + 9
1 + 3 + 9
4 + 9
1 + 1 + 1 + 10
1 + 2 + 10
3 + 10
1 + 1 + 11
2 + 11
1 + 12
this is the result for 13, your code miss a lot of cases

- buptlihongbin October 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include "stdafx.h"
#include "iostream"
#include "map"
#include "list"
#include <sstream>

using namespace std;
class Trick
{
public:
void showallcombinations(int a)
{
for (int i=1; i<=a; i++ )
{
setof2(i);
}
for (int i=2; i<=a-1; i++ )
{
//list <string> temp1 = mapofall[i];
for (std::list<string>::iterator it = mapofall[i].begin(); it != mapofall[i].end(); it++)
{
string tempStr = *it;
tempStr = tempStr + "+ 1";
mapofall[i+1].push_back(tempStr);
}
}
list <string> temp1 = mapofall[a];
for (std::list<string>::iterator it = temp1.begin(); it != temp1.end(); it++)
{
cout << *it << endl;
}
}
private:
int setof2(int a)
{
// i want find all set of 2 to get sum
list<string> locallist;
for (int i=1,j=a-1; i<=j; i++,j-- )
{
if ((i+j) == a)
{
// std::cout << i << "+" << j << std::endl;
stringstream si,sj;
si << i;
sj << j;
string tempStr = si.str()+"+"+sj.str();
locallist.push_back(tempStr);
}
}
mapofall[a] =locallist;
return 0;
}
std::map<int,list<string>> mapofall;
};

int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "HELLO WORLD" << std::endl;
Trick obj;
obj.showallcombinations(10);
int i;
std::cin >> i;
return 0;
}

Output:: [10]
2+8
3+7
4+6
5+5
1+8+ 1
2+7+ 1
3+6+ 1
4+5+ 1
1+7+ 1+ 1
2+6+ 1+ 1
3+5+ 1+ 1
4+4+ 1+ 1
1+6+ 1+ 1+ 1
2+5+ 1+ 1+ 1
3+4+ 1+ 1+ 1
1+5+ 1+ 1+ 1+ 1
2+4+ 1+ 1+ 1+ 1
3+3+ 1+ 1+ 1+ 1
1+4+ 1+ 1+ 1+ 1+ 1
2+3+ 1+ 1+ 1+ 1+ 1
1+3+ 1+ 1+ 1+ 1+ 1+ 1
2+2+ 1+ 1+ 1+ 1+ 1+ 1
1+2+ 1+ 1+ 1+ 1+ 1+ 1+ 1
1+1+ 1+ 1+ 1+ 1+ 1+ 1+ 1+ 1

- Anonymous February 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It still wrong.
It is missing a lot cases. Like 9 +1

2+2+2+2+2

and so on...

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> February 18, 2014 | Flag


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