Join Regular Classroom : Visit ClassroomTech

Salesforce – Overall Interview Questions + Coding Solutions – codewindow.in

Hot Topics

Salesforce Solution

Technical Round

Reverse the words in a string.

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

void reverseWords(string s){
	vector<string> tmp;
	string str = "";
	for (int i = 0; i < s.length(); i++) {
		if (s[i] == ' ') {
			tmp.push_back(str);
			str = "";
		}
		else
			str += s[i];
	}
	tmp.push_back(str);
	int i;
	for (i = tmp.size() - 1; i > 0; i--)
		cout << tmp[i] << " ";
	cout << tmp[0] << endl;
}

int main()
{
	string s = "i like this program very much";
	reverseWords(s);
	return 0;
}
/*
OUTPUT -
much very program this like i
*/

How to find a special weight ball from 8 balls while other 7 have the same weight with a balance? (all same color, shape etc.) Trick is that you don’t know if the special ball is lighter or heavier.

To find the special ball from 8 balls using a balance, you can use the following method:
  1. Divide the 8 balls into two groups of three and a group of two.
  2. Place the two groups of three on the balance scale.
  3. If the two groups are equal in weight, then the special ball must be one of the two remaining balls. Weigh one of these two balls against another ball to determine whether it is lighter or heavier.
  4. If the two groups of three are not equal in weight, the heavier group contains the special ball.
  5. Divide the heavier group of three into two and one, and weigh the two against each other.
  6. If the two are equal in weight, then the special ball is the one left over.
  7. If the two are not equal in weight, the heavier of the two is the special ball.
This method will find the special ball in three weighings. Note that this method assumes that the balance scale is perfectly accurate and that the weight difference between the special ball and the other balls is significant enough to be detected by the scale.

Given a sorted array, where each element but one occurs only once, return the element that repeats. (For example, given 1, 2, 3, 3, 4, return 3. Given 1, 2, 3, 4, 6, 6, 7, 8, 9, return 6.)

Here is one way to solve this problem using a binary search approach in Python:
def repeated_element(arr):
    start, end = 0, len(arr) - 1
    while start <= end:
        mid = (start + end) // 2
        if arr[mid] == arr[mid-1] or arr[mid] == arr[mid+1]:
            return arr[mid]
        elif arr[mid] == mid:
            start = mid + 1
        else:
            end = mid - 1
    return None
This function takes a sorted array arr as input and returns the element that repeats. The basic idea is to use binary search to find a middle element in the array and compare it with its neighboring elements. If the middle element is equal to one of its neighboring elements, then it is the repeating element. If the middle element is equal to its index, then the repeating element must be in the second half of the array, so we update start to mid + 1. If the middle element is not equal to its index or its neighboring elements, then the repeating element must be in the first half of the array, so we update end to mid - 1. The function continues to divide the array in half and check the middle element until the repeating element is found.

Give an array of Integer which contain duplicate number from 1-100, how to count how many distinct number you have?

Here’s one way to count the number of distinct integers in an array using Python:
def count_distinct_numbers(arr):
    return len(set(arr))
This function takes an array arr of integers as input and returns the number of distinct integers in the array by converting the array into a set and then returning the length of the set. Since sets only allow unique elements, converting the array into a set eliminates the duplicates, and the length of the set represents the number of distinct elements in the original array.

What’s the difference between an abstract class and an interface? Can a class inherit both from an abstract class and an interface at the same time?

Interface
Abstract class
1. Interface supports the concept of multiple inheritance.
Abstract class doesn’t support the concept of multiple inheritance.
2. Interface has only abstract methods.
Abstract class can have non-abstract method but it must have at least one abstract method.
3. Interface keyword is used to declare interface
Abstract keyword is used to declare Abstract class.
4. Interface has members as public by default.
Abstract class in Java can have private, protected, public and default class members.
5. Interface supports multiple inheritance.
Abstract class does not support multiple inheritance.

Fastest way to find the middle node in a linked list

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

class Node{
    public:
    int data;
    Node* next;
    Node(int d){
        this->data=d;
        this->next=NULL;
    }
};

void insertAtHead(Node* &head, int d) {

    // new node create
    Node* temp = new Node(d);
    temp -> next = head;
    head = temp;
}

void display(Node* &head) {
    if(head == NULL) {
        cout << "List is empty "<< endl;
        return ;
    }
    Node* temp = head;
    while(temp != NULL ) {
        cout << temp -> data << " ";
        temp = temp -> next;
    }
    cout << endl;
}

Node* middleNode(Node* head){
    if(head==NULL || head->next==NULL){
        return head;
    }
    if(head->next->next==NULL){
        return head->next;
    }
    Node *fast=head,*slow=head;
    while(fast!=NULL){
        fast=fast->next;
        if(fast!=NULL){
            fast=fast->next;
        }
        slow=slow->next;
    }
    return slow;
}

int main() {
    
    Node* node = new Node(10);
    Node* head = node;
    Node* curr = head;
    Node* prev = NULL;
    
    insertAtHead(head,20);
    insertAtHead(head,30);
    insertAtHead(head,40);
    insertAtHead(head,50);
    insertAtHead(head,60);
    
    display(head);
    cout<<head->data<<endl;
    Node* mid=middleNode(head);
    cout<<"Middle node is: "<<mid->data<<endl;

    return 0;
}
/*
OUTPUT 
60 50 40 30 20 10 
60
Middle node is: 30
*/

In java and using math, check if a number is a palindrome.

public static boolean isPalindrome(int num) {
    if (num < 0) {
        return false;
    }
    int reversed = 0, original = num;
    while (num > 0) {
        int digit = num % 10;
        reversed = reversed * 10 + digit;
        num /= 10;
    }
    return reversed == original;
}
In this code, we first check if the input number is negative, in which case we return false because negative numbers can’t be palindromes. Then, we reverse the number using a while loop by extracting each digit, adding it to a variable reversed, and updating num to be num / 10. Finally, we return true if the reversed number is equal to the original number, and false otherwise. This way, we can check if a given number is a palindrome by calling isPalindrome(num).

Given an binary search tree (that is, an ordered tree), how would you find the element of the tree at a particular “index”, where index is defined as the position in the tree’s ordering

To find the element of a binary search tree (BST) at a particular index, you can traverse the tree in-order. In-order traversal of a BST visits the nodes in increasing order, so the node at a particular index can be found by simply counting the nodes during the traversal until the desired index is reached.
Here’s an example implementation in Java:
public TreeNode findIndex(TreeNode root, int index) {
    if (root == null) {
        return null;
    }
    int leftCount = countNodes(root.left);
    if (leftCount == index) {
        return root;
    } else if (leftCount > index) {
        return findIndex(root.left, index);
    } else {
        return findIndex(root.right, index - leftCount - 1);
    }
}

private int countNodes(TreeNode node) {
    if (node == null) {
        return 0;
    }
    return 1 + countNodes(node.left) + countNodes(node.right);
}
In this code, the function findIndex uses a helper function countNodes to count the number of nodes in the left subtree. If the number of nodes in the left subtree is equal to the desired index, then the current node is the desired node. If the number of nodes in the left subtree is greater than the desired index, then the desired node is in the left subtree. If the number of nodes in the left subtree is less than the desired index, then the desired node is in the right subtree, and the desired index is updated to index - leftCount - 1.

Given an NxN matrix, find out the number of ways to get to the NxNth cell from the 1×1 cell.

In a NxN matrix, the number of ways to get to the NxNth cell from the 1×1 cell is equal to the number of paths from (1,1) to (N, N) in the matrix. One possible way to solve this problem is to use dynamic programming.

Implement a stack using a Queue

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

class Stack{
	queue<int> q;
	public:
		void Push(int x){
			//getting the size of queue
			int size=q.size();
			//pushing our element into the queue
			q.push(x);
			//using a for loop to iterate through the elements in queue
			for(int i=0;i<=size-1;i++){
				/*for size one lesser than the size of queue we are
 getting the elements from the front of queue(FIFO) and pushing it back into the same queue.*/
				q.push(q.front());
				//removing the front element from queue
				q.pop();
			}
		}
		
		int Pop(){
			/*getting element from front of queue as we know queue 
			follows FIFO principle*/
			int element=q.front();
			//removing the front element from queue
			q.pop();
			//giving back the element that we removed from queue
			return element;
		}

		int Top(){
			/*the top element in stack will be the first or front element
			of queue*/
			return q.front();
		}
		int Display(){
      			queue<int> p=q;
      			while(!p.empty()){
          			int n=p.front();
          			cout<<n<<endl;
          			p.pop();
      			}
     	 		return 0;
  		}
};//end of class 
	
int main(){
	//s is an object of class Stack on which we will perform our operations
	Stack s;
	//pushing element into the stack
	s.Push(1);
  	s.Push(2);
  	s.Push(3);
  	s.Push(4);

  	cout<<"Displaying the elements in stack"<<endl;
    s.Display();
    cout << "Top element of the stack is: " << s.Top() << endl;
  	cout << "Deleted element is: " << s.Pop() << endl;
	cout<<"Displaying the elements in stack"<<endl;
    s.Display();
  	cout << "Top element of the stack after removing element is: " <<
 	s.Top() << endl;
}
/*
OUTPUT -
Displaying the elements in stack
4
3
2
1
Top element of the stack is: 4
Deleted element is: 4
Displaying the elements in stack
3
2
1
Top element of the stack after removing element is: 3
*/

Interviewer gave me an array of character and asked me to find first non-repeating character

To find the first non-repeating character in an array of characters, you can use a hash map to keep track of the frequency of each character. Here’s an example implementation in Java:
public char firstNonRepeating(char[] chars) {
    Map<Character, Integer> map = new HashMap<>();
    for (char c : chars) {
        map.put(c, map.getOrDefault(c, 0) + 1);
    }
    for (char c : chars) {
        if (map.get(c) == 1) {
            return c;
        }
    }
    return ' ';
}
In this code, we use a hash map map to store the frequency of each character. We iterate over the input array of characters and increment the frequency of each character in the hash map. Then, we iterate over the input array again and return the first character whose frequency is 1 in the hash map. If there is no non-repeating character, we return a space character.

Given 2 unsorted integer arrays, get the intersection of the 2.

To find the intersection of two unsorted integer arrays, you can use a hash set to store elements of one array and then check if each element in the second array exists in the hash set. Here’s an example implementation in Java:
public List<Integer> intersection(int[] nums1, int[] nums2) {
    Set<Integer> set = new HashSet<>();
    List<Integer> result = new ArrayList<>();
    for (int i : nums1) {
        set.add(i);
    }
    for (int i : nums2) {
        if (set.contains(i)) {
            result.add(i);
            set.remove(i);
        }
    }
    return result;
}
In this code, we use a hash set set to store the elements of nums1. Then, we iterate over nums2 and check if each element exists in the hash set. If it does, we add it to the result list and remove it from the hash set to avoid duplicates. The final result is returned as a list of integers.

How to reverse a Linked list

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

class Node{
    public:
    int data;
    Node* next;
    Node(int d){
        this->data=d;
        this->next=NULL;
    }
};

void insertAtHead(Node* &head, int d) {

    // new node create
    Node* temp = new Node(d);
    temp -> next = head;
    head = temp;
}

void print(Node* &head) {
    if(head == NULL) {
        cout << "List is empty "<< endl;
        return ;
    }
    Node* temp = head;
    while(temp != NULL ) {
        cout << temp -> data << " ";
        temp = temp -> next;
    }
    cout << endl;
}

void reverse(Node* &head,Node* curr,Node* prev){
    if(curr==NULL){
        head=prev;
        return ;
    }
    Node* forward=curr->next;
    reverse(head,forward,curr);
    curr->next=prev;
    print(forward);
}

int main() {
    
    Node* node = new Node(10);
    Node* head = node;
    Node* curr = head;
    Node* prev = NULL;
    
    insertAtHead(head,20);
    insertAtHead(head,30);
    insertAtHead(head,40);
    
    print(head);
    cout<<head->data<<endl;
    reverse(head,curr,prev);

    return 0;
}
/*
OUTPUT -
40 30 20 10 
40
List is empty 
*/

What are the differences between C and Java? What would you describe as the defining characteristics of an object-oriented language?

C
Java
  1. C was developed by Dennis Ritchie in 1972.
Java was developed by James Gosling in 1995.
  1. C is a Procedural Programming Language.
Java is Object Oriented Programming Language.
  1. C is broken down to functions.
Java is broken down to objects.
  1. C is a platform dependent language.
Java is a platform independent language.
  1. C supports the concept of pointers.
Java does not support the concept of pointers.
  1. C supports go to statement.
Java does not support the go to statement.
 Important features of OOPs are 
  • Inheritance: It is a concept of Object Oriented Programming in which a new class is created from the existing one.
  • Polymorphism: Poly means many and morph means form. A concept that enables programmers to assign a different meaning or usage to a variable, function or an object in different context.
 
Polymorphism can be of two types:
  1. Runtime Polymorphism
  2. Compile Time Polymorphism
  • Abstraction: Creating a new data type using encapsulated items that is well suited for an application.
  • Encapsulation: It is called data hiding and encapsulation is the way of packing data and function into a single unit to hide the implementation details of a class from outer world.
  • Method Overriding: In place of inheritance if the parent class and child class both have the same method signature then child class method overrides the parent class method.
  • Method Overloading: If a class has more than one method with the same method name with variety of parameter list then method will be overloaded.
  • Object: Object is the reality of a class.
  • Class: Class is a blue print of similar type of objects.
  • Constructor: Constructor is a special member function that is automatically invoked at the time of object creation and does not have any return type.

A bug was found in some code that you wrote, how would you handle this situation?

When a bug is found in code that I wrote, my first step would be to reproduce the bug and understand the problem. This includes gathering information about the inputs and expected outputs, as well as any error messages that may be generated.
Once I have a good understanding of the problem, I would analyze the code to determine the root cause of the bug. I may need to add additional logging statements or test cases to help me pinpoint the issue.
Once I have identified the cause of the bug, I would make the necessary changes to the code to fix the issue and thoroughly test the code to make sure that the changes do not introduce any new bugs or unintended side effects.
If the bug is in code that has already been released, I would need to determine the impact on users and prioritize fixing the bug. I may also need to coordinate with other team members to develop a plan for releasing a fix, such as scheduling a hotfix release or coordinating with customer support to provide a workaround.
Regardless of the specifics, my goal in handling a bug would be to resolve the issue as quickly and efficiently as possible while minimizing any negative impact on users.

Nagarro Solved

Automata Fixing

      

We Love to Support you

Go through our study material. Your Job is awaiting.

Recent Posts
Categories