Engineer1111
BAN USER- 0of 0 votes
AnswerHow to find primes using SQL?
- Engineer1111 in United States
This is the SQL I came up to , but it looks like it'll only get numbers more than the square root
SELECT m1
FROM
(SELECT rownum m1 FROM dual d CONNECT BY LEVEL <1000
)
WHERE NOT EXISTS
(
SELECT x
FROM
(SELECT ROWNUM x FROM DUAL CONNECT BY LEVEL <= (sqrt(1000)))
WHERE x > 1
AND mod(m1,x) = 0
)| Report Duplicate | Flag | PURGE
ADP Applications Developer - 0of 0 votes
AnswersGiven a table with id and value ,
- Engineer1111 in United States
======
id value
======
101 100
102 150
101 80
101 200
102 120
======
Find the median value using SQL. Goes without saying that you can't use MEDIAN function of Oracle SQL.
output:
101 100
102 175| Report Duplicate | Flag | PURGE
Principal Software Engineer - 0of 0 votes
AnswersGeneric Question: You have a list of items that's nearly sorted. What algorithm would you use to completely sort it? Even though it's already sorted, the least element could be at the other end ...eg: 4,5,6,7,8,9,10,11,1
- Engineer1111 in United States
She then said that what would be the approach if the data was not like that, as in , not so extreme.| Report Duplicate | Flag | PURGE
Microsoft Dev Lead Algorithm
list1 = [0,2,3,1]
list2 = [2,4,6,10]
list3 = []
for l in list1:
list3.append(list2[l])
print(list3)
This would depend on the size of the array , but we can use bitwise operators to effectively do it.
Lets assume a couple of helper function:
int numberOfSetBits(unsigned long int a) returns the number of bits that are set to 1
if it is upto 64 people, we can use an unsigned long int in the form of an array
global int a[64];
void addFriend(int userId, int newFriendId)
{
a[userId] = a[userId] |= 1 << x;
}
List<int> GetSuggestedFriends(int UserId)
{
int friend_ctr;
int total_population
for(friend_ctr = 0; friend_ctr < total_population;friend_ctr++)
{
}
}
}
#include <stdio.h>
int find_missing_number(int a[],int size)
{
int i;
int sum = 0;
for(i = 0;i<size;i++)
{
sum -= i;
sum += a[i];
}
return (size - sum);
}
int main()
{
int a[] = {0,1,3,2,4};
int x = find_missing_number(a,sizeof(a)/sizeof(int));
printf("\n missing number is %d",x);
return 0;
}
x = input("please enter integers")
a = x.split(' ')
print(a)
d = {}
for a1 in a:
if(d.get(a1)==None):
d[a1] = 1
else:
print(d)
print('duplicate',a1)
break
#include <stdio.h>
int find_s(int * a, int l)
{
int down = 0;
int up = (l-1);
int mid;
while(down < up)
{
mid = (down+up)/2;
printf("\n mid = %d",mid);
if(a[mid] < a[mid - 1])
return (mid - 1);
else
if(a[mid] > a[down])
down = mid + 1;
else
up = mid ;
}
return -1;
}
int main()
{
int b[5] = {3,4,5,1,2};
int k;
k = find_s(b,5);
printf("\nk = %d\n",k);
return 0;
}
Does it in O(logn) times. If k is provided , then you don't need the algorithm.
- Engineer1111 October 11, 2018#include <stdio.h>
int largest_subset(int * a,int len)
{
int h[10000];
int i,max,curr_max;
for(i=0;i<len;i++)
{
h[a[i]]=1;
}
max = 0;
i = 0;
while(i < 10000)
{
if(h[i] == 1)
{
curr_max = 0;
while(h[i] == 1)
{
curr_max++;
i++;
}
if(curr_max > max)
max = curr_max;
}
i++;
}
return max;
}
int main()
{
int a[10] = {11,4,3,2,1,7,10,8,9};
int i = 9;
int j;
j = largest_subset(a,i);
printf("Largest subset = %d",j);
return 0;
}
select * from customer
where cust_id not in (select cust_id from order1)
select * from customer c1
where not exists (select * from order1 o1 where o1.cust_id = c1.cust_id)
SELECT m1
FROM
(SELECT rownum m1 FROM dual d CONNECT BY LEVEL <1000
)
WHERE NOT EXISTS
(SELECT x
FROM
(SELECT ROWNUM x FROM DUAL CONNECT BY LEVEL <= (sqrt(1000))
)
WHERE x > 1
AND x < m1
AND mod(m1,x) = 0
)
AND m1 > 1;
What you were missing was to add the condition that x < m1. For numbers less than 33 , this would mean the number itself.
- Engineer1111 March 02, 2016int main()
{
int a[] = { 1,2,4,5,6,7 };
int l = 7;
int arith_seq = a[1] - a[0];
int is_seq = 1;
int ctr = 1;
while (ctr < l)
{
//printf("\n ctr = %d arith_seq = %d a[ctr - 1] = %d a[ctr] = %d", ctr,arith_seq,a[ctr-1],a[ctr]);
if (a[ctr] - a[ctr-1] != arith_seq)
{
is_seq = 0;
break;
}
ctr++;
}
if (is_seq) printf("Seq");
else printf("Not seq");
scanf_s("%d", &l);
}
/* Remove duplicates from string given " cutcopypaste " Return "uoyase"
This one removes all duplicates, so aaa will be removed and will not return a*/
#include <stdio.h>
void remove_dup(char * s)
{
char * a = s;
printf("\n s= %s", s);
printf("\n a=%s", a);
short r[256];
int i;
for (i = 0; i < 256; i++)
r[i] = 0;
i = 0;
while (a[i] != '\0')
{
r[a[i]]++;
printf("\n a[%c] is getting visited ", a[i]);
i++;
}
char b[200];
int j = 0;
i = 0;
while (a[i] != '\0')
{
if (r[a[i]] < 2)
b[j++] = a[i];
i++;
}
b[j++] = '\0';
printf("\n String is %s", b);
return;
}
int main()
{
int i;
remove_dup("abcdddddefgabc");
scanf_s("%d", &i);
return 0;
}
{
void mirror(node * n)
{
node * temp;
if (n != NULL)
{
mirror(n->left);
mirror(n->right);
temp = n->left;
n->left = n->right;
n->right = temp;
}
}
}
- Engineer1111 January 31, 2016# Given a sorted array of size N of int32, find an element that repeats > ceil(N / 2) times. #Your algo may assume that there will be always such element. Space/time O(1).
# Follow up question: Now element repeats > ceil(N / 4) times. Space/time O(1)
def find_repeat(a):
b = len(a)
print(b)
if((a[int(b/4)] == a[int(b/2)])or(a[int(b/2)] == a[int(3*b/4)-1])
or(a[int(3*b/4)-1] == a[b-1])):
return 1
else:
return 0
#x = [1,1,1,1,2,2,2,3,3]
x = [1,2,3,4,5,6,7,89]
if(find_repeat(x) == 1):
print("Yes")
else:
print("No")
Sorry - thought that the array is sorted, if it is solution is way straightforward :)
def find_miss(a):
k = len(a)
ctr = 1
seq = 1
while(ctr < k):
if(a[ctr] != seq):
print(seq)
seq = seq + 1
ctr = ctr + 1
seq = seq + 1
a = [0,1,2,3,5,7,8,9,11]
find_miss(a)
I think the class itself will contain a 2 dimensional array .
Since the number of mines is input, can we assume that we can randomly put the requisite number of mines ? Or will that be input too (although it's too cumbersome)
Every cell could have multiple states:
1. Unopened , mine ,
2. Unopened, no mine , with a number
3. Unopened, no mine , with no number (can be treated as zero)
4 . Opened,mine, detected (flag)
5. Opened, no mine , with a number
6. Opened, no mine , with no number (can be treated as zero)
We can create a 2 dim-array where each element is a struct and contains 3 elems
a. mine
b. open
c. neighbor_mines
The constructor could do this:
1. Create a 2-dim array with the above structure.For each element, we can set 1 or 0 by using a random function that'll ensure that number of mines are equal to the input. We can even control that by a loop to ensure that we don't put more mines than input.
2. Loop through the 2 dimensional array and
(a) set mine = decode(random(0,1) < prob,1,0)
(b) set open = 0
3. Loop again and set the neighbor for non mine records mines by summing up 'mine' a[x-1] to a[x+1] and a[y-1] to a[y+1]
I think the above 2 algorithms are great. As an additional performance improvement, if we find the sum is odd, we can directly return false since if the sum of all elements is odd, it is impossible to have 2 numbers in the array whose sum is equal to the sum of the rest of the numbers in the array.
- Engineer1111 July 06, 2014seq {6,6,7} diff is 1 len 3
Clarification- why do we consider 6,6,7 when there is no such continuous sub-array in the array listed ? So it need not be a continuous sequence ?
Should the 3 elements be in the same order at least? If not, then sorting and counting the occurrences seems to be the best idea.
where n is ?
- Engineer1111 April 22, 2014node * find_start(node * n)
{
node * slow = n;
node * fast = n;
do
{
slow = slow->next;
fast = (fast->next)->next;
}
while((slow != fast)&&(fast!=NULL))
if(fast == NULL)
{
printf("No loop found");
return NULL;
}
else
return slow;
}
The idea is to have 2 pointers , with one going at double the speed, at some point the 2 pointers will point to the same node.
If length of loop is even, it'll finish in one iteration, else 2. Either case O(n)
There seems to be a discrepancy in the requirement vs the example:
low numbers in the input array are sorted before all the medium numbers in the input array, which come before all the high numbers in the array.
But in the example, the high numbers are not sorted.
So , finally will the list be sorted or the output expected is
[ (low numbers in any order),(medium numbers in any order), (high numbers in any order)]
?
To remove the non-null elements , you don't need extra space
int i;
int j =0;
for (i = 0;i <n;i++)
{
if(a[i]!=NULL) a[j++] = a[i];
}
In O(n) time, and without using extra space, you've reduced the search space to j elements, then you can probably use linear search . O(n) time,O(1) space
}
To remove the non-null elements , you don't need extra space
int i;
int j =0;
for (i = 0;i <n;i++)
{
if(a[i]!=NULL) a[j++] = a[i];
}
In O(n) time, and without using extra space, you've reduced the search space to j elements, then you can probably use linear search . O(n) time,O(1) space
}
ln(15 billion) - ln(2)
- Engineer1111 January 30, 2014int calcp(int x,int y,int z)
{
int t = 1;
for(int j = 0;j< y;j++)
{
t = (t*x)%z;
}
return t;
}
I think it should be in the heap .
- Engineer1111 April 03, 2013Keep a variable sum and loop through the array - summing up all the elements: sum = sum + a[i] .
Missing number = (n*(n+1)/2) - sum
O(n) complexity .
For case 1: 5.5 is the optimum price. If you want to make profit, charge higher :)
For case 2: 5.5 too
This is making the assumption that once a player picks a number that number is removed from the hat. This would mean however that the last player does not get a chance to put back and pick a card .
If the number is placed back in the hat , then you'll need to charge 10 (for assured profit in case 2) or 5.5 (in case 1)
- Engineer1111 December 18, 2020