Escolar Documentos
Profissional Documentos
Cultura Documentos
Nisarg Dave
2. In a language of your choice, write a code snippet that prints the integers from 1 to 100. If an integer is
a multiple of 3, print Foo instead of the integer. If an integer is a multiple of 5, print Bar instead of the
integer. If the integer is a multiple of 3 and 5, print FooBar instead of the integer.
Coded in C++
For the sake of OOP, wrote more code
Assumed when integer is a multiple of 3 and 5, we are checking for both and not 15. Hence put
the or condition.
#include <iostream>
#include <string>
using namespace std;
class displayNosWithStringReplacements{
public:
void displayWithNoStringReplacement(int n);
void displaywith1StringReplacement(int n, std::string str);
void displaywith2StringReplacements(int n, int m, std::string str);
};
void displayNosWithStringReplacements::displayWithNoStringReplacement(int n){
for (int i = 1; i <= 100; ++i){
cout << i << " ";
}
cout << "\n";
}
void displayNosWithStringReplacements::displaywith1StringReplacement(int n, std::string str){
for (int i = 1; i <= 100; ++i){
if ((i % n) == 0){
cout << str << " ";
}
else{
cout << i << " ";
}
}
cout << "\n";
}
void displayNosWithStringReplacements::displaywith2StringReplacements(int n, int m, std::string str){
for (int i = 1; i <= 100; ++i){
if (((i % n) == 0) || ((i % m) == 0)){
cout << str << " ";
}
else{
cout << i << " ";
}
}
cout << "\n";
}
int main(){
const std::string str = "", str1 = "Foo", str2 = "Bar", str3 = str1 + str2;;
displayNosWithStringReplacements a;
ii
Nisarg Dave
a.displayWithNoStringReplacement(1);
cout << endl;
a.displaywith1StringReplacement(3, str1);
cout << endl;
a.displaywith1StringReplacement(5, str2);
cout << endl;
a.displaywith2StringReplacements(3, 5, str3);
cout << endl;
return 0;
}
3. In hexadecimal, what is the number after F?
10 where 1 is the new digit place and 0 for the completed bit which restarts.
4.
Write a method in either Java or C# that will find the largest value in an array of integers.
using System;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[10];
Random randomNumber = new Random();
Console.WriteLine("The array contain:");
for(int i = 0; i < 10; i++){
arr[i] = randomNumber.Next(1, 100);
Console.WriteLine("{0}", arr[i]);
}
Max(arr);
Console.Read();
Console.Read();
}
static void Max(int [] arr){
int maxNumber = 0;
for (int i = 0; i < arr.Length; i++)
{
if ( arr[maxNumber] < arr[i] )
{
maxNumber = i;
}
}
Console.WriteLine("The maximum number is: {0}", arr[maxNumber]);
iii
Nisarg Dave
}
}
}
5. We have a website that contains 50,000 web pages. Some of those pages contain phone numbers,
which are all either in the form (xxx) xxx-xxxx or xxx xxx-xxxx. All the files are stored on a single
filesystem on a Unix-like machine to which you have access. You have been given the task of producing a
list of all the pages that contain phone numbers. Describe what youd do.
iv
Nisarg Dave
Dec 25 can be coined as decimal 25 = 11001 in binary
Oct 31 can be coined as Octal 31 = 11001 in binary
Thus, Equal.
8. What is the difference between these two operators: & and &&?
& is bitwire AND and && is logical AND (short-circuit AND)
9. You are a consultant who has been hired to write some software for a company that runs parking
garages. After a brief initial discussion with the client, all you know is that they want a system that will
provide insight into the state of all their garages in real time -- how many vehicles are currently in the
garages, the breakdown of cars and trucks (because they pay different rates), where they are in the garages,
how much money theyve made today As you leave the meeting, just for fun you take a moment to
sketch out a very quick diagram showing hierarchy and relationships for some of the major classes in the
system youve started imagining, based on the small amount very high-level info you have.
Draw that hierarchy. Remember, this is a brain dump on a napkin in a hall it doesnt have to be (in fact, it
cant be) perfect, extensive, or complete.
Nisarg Dave
10. Whats good about using a binary tree to find things? Whats the worst-case scenario with a binary
search tree?
Seaching, Insertion and Deletion can be managed efficiently because of its ability to
store keys in the nodes.
They are dynamic and have simple implementation.
The worst time complexity for Indexing, Inserstion, Deletion and Searching as well as
the worst Space Complexity is O(n).
vi