# PySpark Coding Interview Questions & Answers 2024, Get PySpark Interview Questions

## PySpark Coding Interview Questions & Answers 2024 | PySpark Coding Interview Questions | PySpark Interview Questions | PySpark Coding Interview Questions with Solutions

PySpark Coding Interview Questions & Answers 2024: The job seekers are interested to searching the private jobs and off campus drive. The PySpark will be conducted the many coding interview rounds. Aspirants are willing to attend the PySpark Coding Interviews and aimed to get the PySpark Jobs. The PySpark Coding Interview will be intimated through mail. Candidates check the Mail to confirm the interview. The PySpark Coding Interview Questions are asked well known programming languages like C, C++, Java, Python etc., Applicants start your preparation for the PySpark Coding Interview. Must know the PySpark Coding Questions with Answers/ PySpark Interview Questions.

Candidates well prepare PySpark Coding Interview Questions. Those who will be qualified in the PySpark Coding Interview Round, they got the PySpark Jobs. Here we provide all programmes Coding Questions. Candidates use this page to download PySpark Coding Questions. Practice yourself with PySpark Coding Questions with Solutions. Make a schedule for the preparation and must practice through writing. This PySpark Coding Questions and Answers is helping to crack the interview process easily. Candidates focused to prepare the PySpark coding interview with the PySpark Coding Questions and answers.

### PySpark Coding Interview Questions & Answers 2024

Question 1: Alex works at a clothing store. There is a large pile of socks that must be paired by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.

For example, there are n=7 socks with colors ar = {1,2,1,2,1,3,2}. There is one pair of color 1 and one of color 2. There are three odd socks left, one of each color. The number of pairs is 2.

Function Description
Complete the sockMerchant function in the editor below. It must return an integer representing the number of matching pairs of socks that are available.
sockMerchant has the following parameter(s):
n: the number of socks in the pile
ar: the colors of each sock

Input Format
The first line contains an integer n, the number of socks represented in ar.
The second line contains n space-separated integers describing the colors ar[i] of the socks in the pile.

Constraints
1 <= n <= 100
1 <= ar[i] <= 100 & 0 <= i < n

Output Format
Return the total number of matching pairs of socks that Alex can sell.

Sample Input
9
10 20 20 10 10 30 50 10 20
Sample Output
3

Explanation
Alex can match 3 pairs of socks i.e 10-10, 10-10, 20-20
while the left out socks are 50, 60, 20

#### C PROGRAM

```#include<stdio.h>
int sockMerchant(int n, int arr[])
{
int freq[101]={0};
int ans = 0,i;
for(i=0;i<n;i++)
freq[arr[i]]++;
for(i = 0; i <= 100; i++)
{
ans = ans+ freq[i]/2;
}
return ans;
}
int main ()
{
int n;
scanf("%d",&n);
int arr[101],i;
for (i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}

int ans=sockMerchant(n,arr);
printf("%d\n",ans);
return 0;
}```

#### C++ PROGRAM

```#include<bits/stdc++.h>
using namespace std;
int sockMerchant(int n, int arr[])
{
int freq[101]={0};
int ans = 0;
for(int i=0;i<n;i++)
{
int value=arr[i];
freq[value]++;
}
for(int i = 0; i <= 100; i++)
{
ans = ans+ freq[i]/2;
}
return ans;
}
int main ()
{
int n;
cin >> n;
int arr[n]={0};
for (int i = 0; i < n; i++)
{
cin>>arr[i];
}
int res=sockMerchant(n,arr);
cout<<res<<endl;
return 0;
}```

#### JAVA PROGRAM

```import java.util.*;
class Main
{
public static int sockMerchant(int n, int arr[])
{
int freq[]=new int[101];
for(int i=0;i<n;i++)
{
freq[arr[i]]++;
}
int ans=0;
for(int i=0;i<=100;i++)
ans=ans+freq[i]/2;
return ans;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
int ans=sockMerchant(n,arr);
System.out.println(ans);
}
}```

#### PHYTHON PROGRAM

```def sockMerchant(n, ar):
pairs = 0
set_ar = set(ar)
for i in set_ar:
count = ar.count(i)
pairs+=count//2
return pairs

n = int(input())
ar = list(map(int, input().split()))
print(sockMerchant(n, ar))```

Question 2: A left rotation operation on an array shifts each of the array’s elements unit to the left. For example, if 2 left rotations are performed on array [1, 2, 3, 4, 5], then the array would become [3, 4, 5, 1, 2].

Given an array of integers and a number, , perform left rotations on the array. Return the updated array to be printed as a single line of space-separated integers.

Function Description

Complete the function rotLeft in the editor below. It should return the resulting array of integers.

rotLeft has the following parameter(s):

• An array of integers .
• An integer , the number of rotations.

Input Format

The first line contains two space-separated integers and , the size of and the number of left rotations you must perform.

The second line contains space-separated integers a[i].

Constraints

• 1 <= n <= 10^5
• 1 <= d <= n
• 1 <= a[i] <= 10^8

Output Format

Print a single line of space-separated integers denoting the final state of the array after performing d left rotations.

Sample Input
5 4
1 2 3 4 5

Sample Output
5 1 2 3 4

Explanation
When we perform d=4 left rotations, the array undergoes the following sequence of changes:

[1,2,3,4,5] → [2,3,4,5,1] → [3,4,5,1,2] → [4,5,1,2,3] → [5,1,2,3,4]

Test Case : 1

Input (stdin)

• 5 4
• 1 2 3 4 5

Expected Output

• 5 1 2 3 4

Test Case : 2
Input (stdin)

• 20 10
• 41 73 89 7 10 1 59 58 84 77 77 97 58 1 86 58 26 10 86 51

Expected Output

• 77 97 58 1 86 58 26 10 86 51 41 73 89 7 10 1 59 58 84 77

#### C PROGRAM

```#include<stdio.h>
int rotLeft(int arr[], int n, int d)
{
int i, j;
int first;
for(i=0; i<d; i++)
{
first = arr[0];
for(j=0; j<n-1; j++)
{
arr[j] = arr[j+1];
}
arr[j] = first;
}
return *arr;
}
int main()
{
int n, d, i;
scanf("%d",&n);
scanf("%d",&d);
int list[n];
for(i=0; i<n; i++)
{
scanf("%d",&list[i]);
}
rotLeft(list, n, d);
for(i=0; i<n; i++)
{
printf("%d ",list[i]);
}
}```

#### C++ PROGRAM

```#include<bits/stdc++.h>
using namespace std;
int rotLeft(int arr[], int n, int d)
{
int i, j;
int first;
for(i=0; i<d; i++)
{
first = arr[0];
for(j=0; j<n-1; j++)
{
arr[j] = arr[j+1];
}
arr[j] = first;
}
return *arr;
}
int main()
{
int n, d, i;
cin>>n;
cin>>d;
int list[n];
for(i=0; i<n; i++)
{
cin>>list[i];
}
rotLeft(list, n, d);
for(i=0; i<n; i++)
{
cout<<list[i]<<" ";
}
}```

#### JAVA PROGRAM

```import java.util.*;
class Main
{
public static void rotateLeft(int a[],int n, int d)
{
int first,i,j;
for(i=0;i<d;i++)
{
first=a[0];
for(j=0;j<n-1;j++)
a[j]=a[j+1];
a[j]=first;
}
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int d=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
rotateLeft(a,n,d);

for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
}
}```

#### PHYTHON PROGRAM

```def rotateLeft(n,d,arr):
for i in range(d):
arr = rotatearr(n,arr)
return arr

def rotatearr(n, arr):
first = arr[0]
for i in range(n-1):
arr[i] = arr[i+1]
arr[n-1] = first
return arr

, d = map(int, input().split())
arr = list(map(int, input().split()))
for i in rotateLeft(n,d,arr):
print(i, end=" ")```

Question 3:

A Pythagorean triplet is a set of three integers a, b and c such that a2 + b2 = c2. Given a limit, generate all Pythagorean Triples with values smaller than given limit.

```Input : limit = 20
Output : 3 4 5
8 6 10
5 12 13
15 8 17
12 16 20```

Simple Solution is to generate these triplets smaller than given limit using three nested loop. For every triplet, check if Pythagorean condition is true, if true, then print the triplet. Time complexity of this solution is O(limit3) where ‘limit’ is given limit.

An Efficient Solution can print all triplets in O(k) time where k is number of triplets printed. The idea is to use square sum relation of Pythagorean triplet, i.e., addition of squares of a and b is equal to square of c, we can write these number in terms of m and n such that,

```       a = m2 - n2
b = 2 * m * n
c  = m2 + n2
because,
a2 = m4 + n4 – 2 * m2 * n2
b2 = 4 * m2 * n2
c2 = m4 + n4 + 2* m2 * n2
```

We can see that a2 + b2 = c2, so instead of iterating for a, b and c we can iterate for m and n and can generate these triplets

#### C PROGRAM

```//  A C program to generate pythagorean triplets
// smaller than a given limit
#include
#include
//  Function to generate pythagorean triplets
//  smaller than limit
void pythagoreanTriplets (int limit)
{
// triplet:  a^2 + b^2 = c^2
int a, b, c = 0;
//  loop from 2 to max_limitit
int m = 2;
// Limiting c would limit all a, b and c
while (c < limit)
{
// now loop on j from 1 to i-1
for (int n = 1; n < m; ++n)
{
// Evaluate and print triplets using
// the relation between a, b and c
a = m * m - n * n;
b = 2 * m * n;
c = m * m + n * n;
if (c > limit)
break;
printf ("%d %d %d \n", a, b, c);
}
m++;
}
}
// Driver program
int main ()
{
int limit = 20;
pythagoreanTriplets (limit);
return 0;
}```

#### C++ PROGRAM

```#include<bits/stdc++.h>
using namespace std;
//  Function to generate pythagorean triplets
//  smaller than limit
void pythagoreanTriplets (int limit)
{
// triplet:  a^2 + b^2 = c^2
int a, b, c = 0;
//  loop from 2 to max_limitit
int m = 2;
// Limiting c would limit all a, b and c
while (c < limit)
{
// now loop on j from 1 to i-1
for (int n = 1; n < m; ++n)
{
// Evaluate and print triplets using
// the relation between a, b and c
a = m * m - n * n;
b = 2 * m * n;
c = m * m + n * n;
if (c > limit)
break;
cout<<a<<" "<<b<<" "<<c<<endl;
}
m++;
}
}
// Driver program
int main ()
{
int limit = 20;
pythagoreanTriplets (limit);
return 0;
}```

#### JAVA PROGRAM

```import java.util.*;
class Main
{
public static void pythagoreanTriplets(int limit)
{
int a,b,c=0;
int m=2;
while(c<limit)
{
for(int n=1;n<m;++n)
{
a=m*m-n*n;
b=2*m*n;
c=m*m+n*n;
if(c>limit)
break;
System.out.println(a+" "+b+" "+c);
}
m++;
}
}
public static void main(String[] args)
{
int limit=20;
pythagoreanTriplets(limit);
}
}```

#### PHYTHON PROGRAM

```def pythagoreanTriplets(limit):
a = b = c = 0
m = 2
while True:
for n in range(1,m+1):
a = m*m - n*n
b = 2*m*n
c = m*m + n*n
if c > limit:
break
if a==0 or b==0 or c==0:
break
print(a,b,c)
m=m+1

limit = int(input())
pythagoreanTriplets(limit)```

### PySpark Interview Questions and Answers for Freshers

Q) What is PySpark?

This is almost always the first PySpark interview question you will face.

PySpark is the Python API for Spark. It is used to provide collaboration between Spark and Python. PySpark focuses on processing structured and semi-structured data sets and also provides the facility to read data from multiple sources which have different data formats. Along with these features, we can also interface with RDDs (Resilient Distributed Datasets ) using PySpark. All these features are implemented using the py4j library.

Q) What are the various algorithms supported in PySpark?

The different algorithms supported by PySpark are:

• spark.mllib
• mllib.clustering
• mllib.classification
• mllib.regression
• mllib.recommendation
• mllib.linalg
• mllib.fpm

Q) What is PySpark StorageLevel?

PySpark StorageLevel is used to control how the RDD is stored, take decisions on where the RDD will be stored (on memory or over the disk or both), and whether we need to replicate the RDD partitions or to serialize the RDD. The code for StorageLevel is as follows:

class pyspark.StorageLevel( useDisk, useMemory, useOfHeap, deserialized, replication = 1)

Q) What is PySpark SparkJobinfo?

One of the most common questions in any PySpark interview. PySpark SparkJobinfo is used to gain information about the SparkJobs that are in execution. The code for using the SparkJobInfo is as follows:

class SparkJobInfo(namedtuple(“SparkJobInfo”, “jobId stageIds status ”)):

Q) What is PySpark Join?

PySpark Join helps combine two DataFrames. By binding these, it is easy to join multiple DataFrames. It enables all fundamental join type operations accessible in traditional SQL like INNER, RIGHT OUTER, LEFT OUTER, LEFT SEMI, LEFT ANTI, SELF JOIN, and CROSS. PySpark Joins are transformations that use data shuffling throughout the network.

The dailyrecruitment.in web page is helpful to learn and got an information about jobs and educational related updates.

### Govt Jobs by Qualifications

Education & Vacancies Salary Apply Link
10th Pass Govt Jobs - 5,000 Vacancies Rs. 5,200 - 63,200 Apply Now
12th Pass Govt Jobs - 18,000+ Vacancies Rs. 5,200 - 92,300 Apply Now
ITI Pass Jobs - 3,500 Vacancies Rs. 5,200 - 35,000 Apply Now
Any Graduate Jobs - 19,100 Vacancies Rs. 5,200 - 92,300 Apply Now
Central Govt Jobs Rs. 5,200 - 17,000 Apply Now
Bank Jobs - 1,000 Vacancies Rs. 5,200 - 29,200 Apply Now
Diploma Jobs - 9,300 Vacancies Rs. 5,200 - 35,000 Apply Now
BTech/BE Jobs - 18,000 Vacancies Rs. 15,000 - 1,00,000 Apply Now
Data Entry Jobs - 1,300 Vacancies Rs. 5,200 - 29,200 Apply Now
Private Jobs Rs. 10,000 - 67,700 Apply Now