Hot Topics
Adobe Solution
Technical Round
- Question 1
There is an array of size n which consists of any numbers from 1 to n .Find the numbers in the array which are appear more than once.
- Answer
#include <stdio.h>
void printDuplicates(int arr[], int n) {
int i;
int count[n];
// Initialize count array with 0
for (i = 0; i < n; i++)
count[i] = 0;
// Traverse the original array and update count[]
for (i = 0; i < n; i++) {
if (count[arr[i] - 1] == 1)
printf("%d ", arr[i]);
else
count[arr[i] - 1]++;
}
}
int main() {
int arr[] = {1, 3, 2, 1, 4, 2};
int n = sizeof(arr) / sizeof(arr[0]);
printDuplicates(arr, n);
return 0;
}
/*
OUTPUT
1 2
*/
- Question 2
Design a search system for a document search.
- Answer
Here is an approach to design a search system for a document search:
Data Collection: The first step is to collect the documents that you want to search through. You can either have the documents in a database or on the file system.
Data Preprocessing: The next step is to preprocess the data. This involves removing stop words, stemming words, and transforming the text data into a format that is easier to search.
Indexing: The preprocessed data must be transformed into a searchable data structure. One common approach is to use an inverted index. An inverted index is a data structure that maps each unique word in the document to a list of documents that contain that word.
Searching: When a user performs a search, the system will take the search query and use it to look up the relevant documents in the inverted index. The system will then rank the documents based on the number of occurrences of the search query words and return the most relevant results.
Relevance Ranking: The system must determine the relevance of the documents returned by the search. This can be done using various ranking algorithms, such as TF-IDF (term frequency-inverse document frequency) or BM25 (Best Matching 25).
User Interface: Finally, the system should provide a user-friendly interface to allow users to perform searches and view the results.
This is a high-level overview of the process of designing a search system for document search. Depending on the specific requirements, additional steps or optimizations may be necessary.
- Question 3
Iterative method to find the height of a tree.
- Answer
The height of a tree can be found using an iterative method by performing a level-order traversal of the tree. The height of a tree is defined as the number of edges in the longest path from the root to a leaf node.
Here is the implementation of an iterative method to find the height of a binary tree:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
int height(struct Node* root) {
if (root == NULL)
return 0;
int height = 0;
int level_nodes = 0;
struct Node* queue[10000];
int front = 0;
int rear = 0;
queue[rear++] = root;
while (front != rear) {
level_nodes = rear - front;
height++;
while (level_nodes > 0) {
struct Node* current = queue[front++];
if (current->left != NULL)
queue[rear++] = current->left;
if (current->right != NULL)
queue[rear++] = current->right;
level_nodes--;
}
}
return height;
}
In this implementation, we use a queue to store the nodes at each level of the tree. The height of the tree is equal to the number of levels, which is equal to the number of times the while loop runs. The time complexity of this algorithm is O(n), where n is the number of nodes in the tree.
- Question 4
Given an array of n numbers, return an array ans such that ans[i] contains the product of all elements in array except array[i]. Ex. array = {1, 7, 4, 3}, ans = {84, 12, 21, 28}. Do this in O(n) time and without using division.
- Answer
#include <stdio.h>
#include <stdlib.h>
void productArray(int array[], int n, int ans[]) {
int left_product[n];
int right_product[n];
left_product[0] = 1;
right_product[n - 1] = 1;
for (int i = 1; i < n; i++) {
left_product[i] = left_product[i - 1] * array[i - 1];
}
for (int i = n - 2; i >= 0; i--) {
right_product[i] = right_product[i + 1] * array[i + 1];
}
for (int i = 0; i < n; i++) {
ans[i] = left_product[i] * right_product[i];
}
}
int main() {
int array[] = {1, 7, 4, 3};
int n = sizeof(array) / sizeof(array[0]);
int ans[n];
productArray(array, n, ans);
printf("The product array is: ");
for (int i = 0; i < n; i++) {
printf("%d ", ans[i]);
}
return 0;
}
/*
OUTPUT
The product array is: 84 12 21 28
*/
- Question 5
Given any number, describe an algorithm that increments that number enough to turn the result into a number palindrome (i.e. reads the same left to right and right to left). That is, an input of 1000 would have a palindrome output of 1001. 9019 would have an output of 9119. If the input is already a palindrome, find the next highest palindrome.
- Answer
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool isPalindrome(char number[], int len) {
for (int i = 0; i < len / 2; i++) {
if (number[i] != number[len - i - 1]) {
return false;
}
}
return true;
}
void increment(char number[], int len) {
int carry = 1;
for (int i = len - 1; i >= 0; i--) {
int sum = (number[i] - '0') + carry;
if (sum >= 10) {
carry = 1;
sum = 0;
} else {
carry = 0;
}
number[i] = sum + '0';
}
}
void nextPalindrome(char number[], int len) {
increment(number, len);
while (!isPalindrome(number, len)) {
increment(number, len);
}
}
int main() {
char number[] = "1001";
int len = strlen(number);
nextPalindrome(number, len);
printf("The next highest palindrome is: %s\n", number);
return 0;
}
/*
OUTPUT
The next highest palindrome is: 1111
*/
- Question 6
Find Loop in a Binary Tree.
- Answer
To detect a loop in a binary tree, one approach is to use a depth-first search (DFS) algorithm and keep track of the nodes we’ve visited. If we visit a node that we’ve already visited before, it means there’s a loop in the tree. Here’s an example implementation in C:
#include <stdbool.h>
#include <stdio.h>
struct Node {
int value;
struct Node *left;
struct Node *right;
bool visited;
};
bool detectLoop(struct Node *node) {
if (node == NULL) {
return false;
}
if (node->visited) {
return true;
}
node->visited = true;
if (detectLoop(node->left) || detectLoop(node->right)) {
return true;
}
node->visited = false;
return false;
}
int main() {
struct Node a, b, c, d, e, f, g;
a.value = 1;
b.value = 2;
c.value = 3;
d.value = 4;
e.value = 5;
f.value = 6;
g.value = 7;
a.left = &b;
a.right = &c;
b.left = &d;
b.right = &e;
d.left = &f;
d.right = &g;
c.left = &g;
c.right = &b;
b.visited = false;
c.visited = false;
d.visited = false;
e.visited = false;
f.visited = false;
g.visited = false;
if (detectLoop(&a)) {
printf("There is a loop in the binary tree.\n");
} else {
printf("There is no loop in the binary tree.\n");
}
return 0;
}
/*
OUTPUT
There is a loop in the binary tree.
*/
- Question 7
How would you sort a list of strings with numbers written in English by their value?
- Answer
One approach to sort a list of strings with numbers written in English by their value is to use a custom sorting function that converts each string to a number and compares the numbers instead of the strings. Here’s an example implementation in Python:
import re
def convert_to_number(word):
number_map = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
"ten": 10
}
return number_map.get(word, 0)
def sort_by_value(strings):
def sort_key(string):
words = re.findall(r'\b\w+\b', string)
numbers = [convert_to_number(word) for word in words]
return sum(numbers)
return sorted(strings, key=sort_key)
strings = ["ten two", "four five", "three one", "seven ten", "seven six", "nine three"]
print(sort_by_value(strings))
#OUTPUT- ['three one', 'four five', 'ten two', 'nine three', 'seven six', 'seven ten']
- Question 8
Difference between logit and probit models.
- Answer
Logit and Probit models are both statistical models used to model the relationship between a binary dependent variable and one or more independent variables.
The logit model uses the logistic function to model the relationship between the dependent and independent variables. The logistic function maps any real number to the range of 0 to 1, making it useful for modeling binary outcomes. The logit model is a type of generalized linear model that is used to model binary outcomes, such as the probability of success or failure.
On the other hand, the Probit model uses the cumulative distribution function (CDF) of the standard normal distribution to model the relationship between the dependent and independent variables. The Probit model is also used to model binary outcomes, but it assumes that the errors in the model follow a normal distribution.
In summary, the main difference between logit and Probit models is the type of function used to model the relationship between the dependent and independent variables. The logit model uses the logistic function, while the Probit model uses the CDF of the normal distribution. Both models have their own advantages and disadvantages and the choice of which model to use depends on the specific problem being analyzed and the assumptions of the data.
- Question 9
Write a function that removes substrings.
- Answer
#include <iostream>
#include <string>
std::string remove_substring(const std::string &str, const std::string &sub) {
std::string result = "";
std::string::size_type n = sub.length();
for (std::string::size_type i = 0; i < str.length();) {
if (str.substr(i, n) == sub) {
i += n;
} else {
result += str[i++];
}
}
return result;
}
int main() {
std::string str = "Hello, world!";
std::string sub = "world";
std::cout << remove_substring(str, sub) << std::endl;
return 0;
}
/*
OUTPUT
Hello, !
*/
- Question 10
Is JavaScript a scripting or object-oriented language?
- Answer
JavaScript is considered a multi-paradigm language as it supports both scripting and object-oriented programming.
Scripting refers to the use of programming languages to write code to automate tasks, such as creating dynamic web pages or automating repetitive processes. JavaScript is often used as a scripting language because of its ability to manipulate HTML and CSS in real-time, and because it can be embedded directly into web pages to provide interactivity and dynamic content.
Object-oriented programming (OOP) is a programming paradigm that is based on the concept of objects. Objects are instances of classes that contain data (attributes) and behaviors (methods). JavaScript has object-oriented capabilities, including inheritance, encapsulation, and polymorphism, and supports the creation of classes, objects, and prototypes.
In summary, JavaScript is a multi-paradigm language that supports both scripting and object-oriented programming, making it a flexible and versatile language for web development and beyond.
- Question 11
Asynchronous Library calls in javascript.
- Answer
Asynchronous library calls in JavaScript are calls that do not block the execution of the program while they are being processed. This means that while an asynchronous call is being executed, the rest of the program can continue to run, leading to improved performance and responsiveness.
JavaScript uses asynchronous programming through the use of callbacks and promises. Callbacks are functions that are passed as arguments to other functions and are executed after the main function has completed. Promises are objects that represent the eventual completion or failure of an asynchronous operation.