Skip to content

Commit

Permalink
Cloudera Drive Coding and Interview Experience
Browse files Browse the repository at this point in the history
  • Loading branch information
satyajeetramnit committed Aug 24, 2022
1 parent 7a6fb6b commit c7b0a92
Show file tree
Hide file tree
Showing 7 changed files with 435 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Cloudera/Drive 14.08.2022/.vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/MinGW/bin/gcc.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
85 changes: 85 additions & 0 deletions Cloudera/Drive 14.08.2022/closestRandomPoints.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Closest Random Points

// In many real world applications the problem of finding a pair of closest points arises.
// In the real world, data is usually distributed randomly. Given n points on a plane that are randomly generated with uniform distribution,
// find the squared shortest distance between pairs of these points.

// Example
// There are 3 points with x coordinates x = [0, 1, 2] and y=[0, 1, 4].
// The points have the xy coordinates (0, 0), (1, 1) and (2, 4).
// The closest points are (0,0) and (1, 1), and their squared shortest distance is (1-0)²+(1-0)² = 2.

// Function Description
// Complete the function closestSquaredDistance in the editor below.

// closestSquaredDistance has the following parameter(s):
// int x[n]: each x[i] denotes the x coordinate of the ith point
// int y[n]: each y[i] denotes the y coordinate of the ith point

// Returns: long: a long integer that denotes the squared

#include<bits/stdc++.h>
using namespace std;

// Method 1
// long closestSquaredDistance(vector<int> x,vector<int> y){
// int n = x.size();
// if(n==0) return 0;
// vector<pair<int,int>> v;
// for(int i=0;i<n;i++){
// v.push_back({x[i],y[i]});
// }
// sort(v.begin(),v.end());
// long ans = LONG_MAX;

// for(int i=0;i<n;i++){
// for(int j=i+1;j<n;j++){
// long x1 = v[i].first;
// long y1 = v[i].second;
// long x2 = v[j].first;
// long y2 = v[j].second;
// long dist = abs((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2));
// // cout<<dist<<endl;
// if(dist<ans){
// ans = dist;
// }
// }
// }
// return ans;
// }

// Method 2
long closestSquaredDistance(vector<int> x,vector<int> y){
int n = x.size();
if(n==0) return 0;
vector<pair<int,int>> v;
for(int i=0;i<n;i++){
v.push_back({x[i],y[i]});
}
sort(v.begin(),v.end());
long ans = LONG_MAX;

for(int i=0;i<n;i++){
long x1 = v[i].first-v[i-1].first;
long y1 = v[i].second-v[i-1].second;
long dist = abs((x1) * (x1) + (y1) * (y1));
if(dist<ans){
ans = dist;
}
}
return ans;
}


int main(){
vector<int> x = {543243,5000};
vector<int> y = {0,322};

// vector<int> x={77,1000,992,1000000};
// vector<int> y={0,1000,500,0};

// vector<int> x={0,10,15};
// vector<int> y={0,10,20};
cout<<closestSquaredDistance(x,y);
return 0;
}
34 changes: 34 additions & 0 deletions Cloudera/Drive 14.08.2022/deleteEven.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<bits/stdc++.h>
using namespace std;

class SinglyLinkedListNode{
public:
int data;
SinglyLinkedListNode* next;
SinglyLinkedListNode(int node_data){
this->data = node_data;
this->next = nullptr;
}
};

SinglyLinkedListNode* deleteEven(SinglyLinkedListNode* listHead){
SinglyLinkedListNode* head = new SinglyLinkedListNode(0);
SinglyLinkedListNode* temp = head;
while(listHead){
if(listHead->data % 2 != 0){
temp->next = listHead;
temp = temp->next;
}
listHead = listHead->next;
}
temp->next = nullptr;
return head->next;
}

void print_list(SinglyLinkedListNode* node){
while(node){
cout<<node->data<<" ";
node = node->next;
}
cout<<endl;
}
154 changes: 154 additions & 0 deletions Cloudera/Drive 14.08.2022/maximizeArrayValue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Maximize Array Value

// Given an array arr of n positive integers, the following operation can be performed any number of times. Use a 1-based index for the array.
// • Choose any i such that 2 <= i <= n.
// • Choose any x such that 1 <= x <= arr[i]
// • Set arr[i-1]to arr[i-1] + x
// • Set arr[i] to arr[i] - x

// Minimize the maximum value of arr using the operation and return the value.

// Example
// n=4
// arr = [1, 5, 7, 6]

// Assuming 1-based indexing.

// One optimal sequences is:

// • Operation 1: choose i=3, x=4 (note that x <= arr[3], i.e. 4<7).
// • Replace arr[i-1] with arr[i-1]+x or 5+ 4 = 9
// • Replace arr[i] with arr[i]-x or 7-4=3
// • The array is now [1, 9, 3, 6] (maximum = 9)

// • Operation 2: i=2,x=4
// • Replace arr[2-1] with 1 + 4 = 5
// • Replace arr[2] with 9 - 4 = 5
// • The array is now [5, 5, 3, 6] (maximum = 6)

// • Operation 3: i = 4, x= 1, the resulting array is [5, 5, 4, 5] (maximum = 5)

// The minimum possible value of max(arr) is 5 after operation 3.


// Function Description
// Complete the function getMaximum in the editor below.

// getMaximum has the following parameter:
// int arr[n]: an array of integers

// Returns
// int: the minimum maximum value possible


#include<bits/stdc++.h>
using namespace std;

// Method 1
// In CPP
int getMaximum(vector<int> arr) {
int n = arr.size(), res=arr[0], x=1, pre=arr[0];
long long sum=pre;
for(int i=1;i<n;i++){
if(arr[i]>=pre){
x++;
pre=arr[i];
sum+=pre;
}
else{
int avg = (int)(ceil((sum * 1.0) / x));
res = max(res, avg);
x=1;
pre=arr[0];
sum=pre;
}
}
int avg = (int)(ceil((sum * 1.0) / x));
res = max(res, avg);
return res;
}


// In Java
// public static int getMaximum(List<Integer> arr) {
// int ans = arr.get(0);
// int n = arr.size();
// int k = 1;
// int past = arr.get(0);
// long sum = past;
// for(int i=1;i<n;i++) {
// if(arr.get(i) >= past) {
// k++;
// past = arr.get(i);
// sum += past;
// }
// else {
// int avg = (int)(Math.ceil((sum * 1.0) / k));
// ans = Math.max(ans, avg);
// k = 1;
// past = arr.get(0);
// sum = past;
// }
// }
// int avg = (int)(Math.ceil((sum * 1.0) / k));
// ans = Math.max(ans, avg);
// return ans;
// }


// Method 2
// In CPP
// int getMaximum(vector<int> arr) {
// int n = arr.size();
// int ans = arr[0];
// for(int i=1;i<n;i++) {
// int diff = arr[i] - arr[i-1];
// if(diff > 0) {
// if(diff % 2 == 0) {
// arr.insert(arr.begin() + i - 1, arr[i-1] + (diff / 2));
// ans = max(ans, arr[i-1]);
// arr.insert(arr.begin() + i, arr[i] + (diff / 2));
// ans = max(ans, arr[i]);
// }
// else if(diff % 2 == 1) {
// arr.insert(arr.begin() + i - 1, arr[i-1] + (diff / 2));
// ans = max(ans, arr[i-1]);
// arr.insert(arr.begin() + i, arr[i] + (diff / 2)+1);
// ans = max(ans, arr[i]);
// }
// }
// }
// return ans;
// }

// In Java
// public static int getMaximum(List<Integer> arr) {
// int ans = arr.get(0);
// int n = arr.size();
// for(int i=1;i<n;i++) {
// int diff = arr.get(i) - arr.get(i-1);
// if(diff > 0) {
// if(diff % 2 == 0) {
// arr.add(i-1, arr.get(i-1) + (diff / 2));
// ans = Math.max(ans, arr.get(i-1));
// arr.add(i, arr.get(i) + (diff / 2));
// ans = Math.max(ans, arr.get(i));
// }
// else if(diff % 2 == 1) {
// arr.add(i-1, arr.get(i-1) + (diff / 2));
// ans = Math.max(ans, arr.get(i-1));
// arr.add(i, arr.get(i) + (diff / 2)+1);
// ans = Math.max(ans, arr.get(i));
// }
// }
// }
// return ans;
// }


int main(){
vector<int> arr = {10,3,5,7};
cout<<getMaximum(arr);
// cout<<maximumArrayValue(arr);
return 0;
}
76 changes: 76 additions & 0 deletions Cloudera/Drive 14.08.2022/simpleMatrixSummation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Simple Matrix Summation

// Given an nxm matrix, a, where each a[i,j] is the value of the cell at the intersection of row and column,
// create another nxm matrix, b, using the following algorithm to set the values of each b(x, y) with zero-based indexing

/*
s=0
for(i=0;i<=x;i+=1){
for(j=0;j<=y;j+=1){
s+=a[i,j]
}
}
b[x,y]=s
*/

// Note that the algorithm sets just one b(x, y) per execution and that 0 ≤ x < n and 0 ≤ y < m.
// It should be run for each element of b. Return the completed nxm matrix b.

// Function Description
// Complete the function findMatrix in the editor below.

// findMatrix has the following parameter(s):
// a[a[0],....a[n-1]]: a 2-dimensional array of integers

// Returns:
// int: an n x m(2-dimensional) matrix of integers denoting b

// Example
// If Matrix a is
// 1 2 3
// 4 5 6

// The final Matrix b is
// 1 3 6
// 5 12 21

#include<bits/stdc++.h>
using namespace std;

vector<vector<int>> findMatrix(vector<vector<int>> a) {
int n = a.size(), m = a[0].size();
vector<vector<int>> b(n, vector<int>(m));
b[0][0] = a[0][0];
for(int i = 1; i < n; i++)
b[i][0] = a[i][0] + b[i - 1][0];
for(int j = 1; j < m; j++)
b[0][j] = a[0][j] + b[0][j - 1];
for(int i = 1; i < n; i++) {
for(int j = 1; j < m; j++)
b[i][j] = a[i][j] + b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
}
return b;
}

int main(){
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
vector<vector<int>> b = findMatrix(a);

for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cout << b[i][j] << " ";
}
cout << endl;
}

return 0;
}
Loading

0 comments on commit c7b0a92

Please sign in to comment.