Answer For Questions

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

1.

Voting
Each voter can vote in zero or more referenda. Each referendum has one or more questions, and
each question is a yes/no vote. Write the simplest normalized schema to describe this in generic
SQL statements or as an entity-relationship diagram. Point out where the indexes would be if
you want to quickly know the results of a given referendum question, but you never expect to
query a single voter's voting record. In what way does the nature of the application affect your
design judgment about privacy, and what effect would that have on normalization
considerations?

Answer:

2. Rewriting git with history


You've getting ready to submit a pull request to the coolapp repo. You branch contains two
commits: M, followed by N. The lineage is this:

main: X --> Y --> Z (HEAD commit is Z)


\
your-branch: M --> N (HEAD commit is N)
You want to contribute the code back to the official coolapp repo, but you realize there is a
really dumb typo in one of your source code comments (not commit messages) in commit M.
You'd still like to submit the pull request as two commits M' and N', where M' is the fixed
version of M, and N' is the same exact diff as N. How do you rewrite git history to make this
happen? Is N' the same hash as N? Why or why not?

Answer:

First we have to move to the coolapp repo, and write the following command:

$: git checkout “coolapp branch”

Then change the commit message of the last commit, means “M”, by using the following
command:

$: git commit amend

3. Service Container
Consider the following Laravel controller:

class MyController extends Controller


{
private $provider;

public function __construct(MyContentProvider $provider)


{
$this->provider = $provider;
}

public function getContent()


{
return view('index', [ 'content' => $this->provider->get() ]);
}
}
When this controller is instantiated, a fresh instance of MyContentProvider is passed to the
controller without any other code changes. How does Laravel know how to construct the
Controller? What if MyServiceProvider has dependencies on its own? Explain how this works.
Answer:
The laravel controller is a powerful tool for managing class dependencies and performing
dependency injection. Dependency injection are injected into the class through the constructor
or in some cases using setter methods.

In the above code the MyController needs to retrieve provider from a data source. So we will
inject a service that is able to retrieve provider. In this context, our MyContentProvider most likely
uses Eloquent (an object-relational mapper (ORM) that makes it enjoyable to interact with the
database) to retrieve provider information from the database. However, since the repository is
injected, we are able to easily swap it out with another implementation.
4. We’re All Scratching Our Heads About This One
New code was released to multi-tier production environment and now the site is SO slow. But it
worked great on the single-server staging environment, which has only half the CPU and half
the RAM of the production servers!

Here's the code that's behaving poorly:

class Post extends Model


{
public function comments()
{
$comment_ids = array_map(function ($comment) { return $comment->id; }, Comment::where("post_id",
$this->id)->get()->toArray());
return array_map(static function ($comment_id) { Comment::find($comment_id); }, $comment_ids);
}
}
What's wrong? And why did it work well (enough) on the staging server?

Obviously the person who wrote this isn't a Laravel programmer. How would a Laravel
programmer have written the code?

Answer:

Query1: $comment_ids = array_map(function ($comment) { return $comment->id; },


Comment::where("post_id",$this ->id)->get()->toArray());

 This code calculates the ids of all comments and put it in array

Query2: array_map(static function ($comment_id){ Comment::find($comment_id); }, $comment_ids);

 This code loops over the array and finds the post comment one by one

It makes the process slow in multi-tier production environment because client and database are

in different location. The client makes request to the server and the server executes the above
code and fetch data from database. While in staging environment, client and server are in single

location that’s why it executes faster.


class Post extends Model
{
Public function comments()
{
Return $this->hasMany(‘App\Models\Comment’);
}
}
Class Comment extends Model
{
Public function post()
{
Return $this->belongsto(‘App\Models\Post’);
}
}
5. Unix tools
In one Unix command, find all of the files in /tmp whose contents contain the word "foobar"

(case-insensitive), and list them all in order from most-recently created to least-recently created.
Answer:
$: find /tmp –type f –iname “*foobar*” –print

6. CSS
Usage of the "!important" CSS declaration is often frowned upon by front-end developers. What does the
"!important" CSS declaration do? Why do front-end developers suggest using it with caution? What are
some cases in which it makes sense to use it?
Answer:

The ‘!important’ role in CSS is used to override all previous styling rules for that specific property on that
element.

For example:
<html>
<head>
<style>
#myId {
Background-color: yellow;
}
.myClass {
Background-color: blue;
}
P {
Background-color: red !important;
}
</style>
</head>
<body>
<p>Inside paragraph</p>
<p class="myClass">Inside paragraph and myClass</p>
<p id="myId”>Inside paragraph and myId</p>
</body>
</html>
In this example all the three paragraph will get red background color.

Why do front-end developers suggest using it with caution?


 Because using !important will overrides all the CSS styling rules for that specific

property on that element.


What are some cases in which it makes sense to use it?
 If the style cannot be overridden in any other way. This could be if you are working on
a CMS (Content Management System) source code. However do not use it unless you

absolutely have to.


 If you want special look for all buttons on the page.

7. JavaScript
Take a look at the following:

function *foo(x) {
while (x < 4) {
x += yield x;
}
return x;
}
var bar = foo(3);
console.log( bar.next(1) );
console.log( bar.next(1) );
console.log( bar.next(1) );
What is the output of this code snippet, and why does it behave in the way it does?

Answer:

Output:

{value: 3, done: false}

{value: 4, done: true}

{value: undefined, done: true}

Yield: this keyword is used to pause and resume the generator function.

The answer we get when we call the function ‘foo’ for the first time is
{value: 3, done: false}, because we initialize our x value to 3 and the yield
keyword pauses the function, so we get value: 3 and the loop iteration is not
completed so we are not done yet, so we get done: false.

The answer we get when we call the function ‘foo’ for the second time
is {value: 4, done: true], because the next function asks to increase
the value of x by one, so we get value: 4 and the loop iteration is
completed, so we get done: true, now we are done in the loop.

The answer we get when we call the function ‘foo’ for the third time
is {value: undefined, done: true}, because the loop is done so we get
undefined value.

You might also like