Вы находитесь на странице: 1из 6

Question 1:

Problem: There is a colony of 8 cells arranged in a straight line where each day
every cell competes with its adjacent cells(neighbour). Each day, for each cell, if
its neighbours are both active or both inactive, the cell becomes inactive the next
day, otherwise it becomes active the next day. (Java)

Assumptions:
The two cells on the ends have single adjacent cell, so the other adjacent cell can
be assumsed to be always inactive.
Even after updating the cell state. consider its previous state for updating the state
of other cells. Update the cell information of all cells simultaneously.
Write a function cellCompete which takes takes one 8 element array of integers
cells representing the current state of 8 cells and one integer days representing te
number of days to simulate.
An integer value of 1 represents an active cell and value of 0 represents an inactive
cell.

program:
int* cellCompete(int* cells,int days)
{/
/write your code here
}
//function signature ends
TESTCASES 1:
INPUT:
[1,0,0,0,0,1,0,0],1
EXPECTED RETURN VALUE:
[0,1,0,0,1,0,1,0]
TESTCASE 2:
INPUT:
[1,1,1,0,1,1,1,1,],2
EXPECTED RETURN VALUE:
[0,0,0,0,0,1,1,0]
Solution:

class
Colony
{
public static int[] cellCompete(int[] cells, int days)
{
// INSERT YOUR CODE HERE
int len = cells.length;
int [] newCells = new int[cells.length];
for(int k = 0; k < days; k++) {
for (int i = 0; i < cells.length; i++) {
int cell = cells[i];
int nextCell;
int prevCell;
int activenumber;
if (i == 0) {
// edge cases
nextCell = cells[1];
prevCell = 0;
} else if (i == cells.length - 1) {
// edge case
prevCell = cells[cells.length - 2];
nextCell = 0;
} else {
nextCell = cells[i + 1];
prevCell = cells[i - 1];
}
if (nextCell == prevCell) {
// set it to inactive
activenumber = 0;
} else {
//set it to active
activenumber = 1;
}
newCells[i] = activenumber;
}
for (int i = 0; i < 8; i++) {
cells[i] = newCells[i];
}
}
return newCells;
}
public static void main(String[] args) {
int[] array = {1,1,1,0,1,1,1,1};
int days = 2;
array = cellCompete(array, days);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
}
}
}

Question 2: Find the GCD of two numbers in C

Input: Two Integers a and b


Output: An Integer representing gcd of a and b

Solution:
include <stdio.h>

int gcd(int a, int b)


{
if (a == 0)
return b;
return gcd(b%a, a);
}

int main()
{
int a = 10, b = 15;
printf("GCD(%d, %d) = %d\n", a, b, gcd(a, b));
a = 35, b = 10;
printf("GCD(%d, %d) = %d\n", a, b, gcd(a, b));
a = 31, b = 2;
printf("GCD(%d, %d) = %d\n", a, b, gcd(a, b));
return 0;
}
Question 3:
Problem: The Least-Recently-Used(LRU) cache algorithm exists the
element from the cache(when it's full) that was least-recently-used. After
an element is requested from the cache, it should be added to the cache(if
not already there) and considered the most-recently-used element in the
cache.

Given the maximum size of the cache and a list of integers(to request
from the cache), calculate the number of cache misses using the LRU
cache algorithm. A cache miss occur when the requested integer does not
exist in the cache.
Initially, the cache is empty.
The input to the function LruCountMiss shall consist of an integer
max_cache_size, an array pages and its length len.
The function should return an integer for the number of cache misses
using the LRU cache algorithm.
Assume that the array pages always has pages numbered from 1 to 50.
TESTCASES:
TESTCASE1:
INPUT:
3,[7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0],16
EXPECTED RETURN VALUE:
11
TESTCASE 2:
INPUT:
2,[2,3,1,3,2,1,4,3,2],9
EXPECTED RETURN VALUE:
8
EXPLANATION:
The following page numbers are missed one after the other
2,3,1,2,1,4,3,2.This results in 8 page misses.
CODE:
int lruCountMiss(int max_cache_size, int *pages,int len)
{
//write tour code
}
Solution:

import java.util.Stack;

public class LeastRecentPage


{
private static Stack<Integer> cache;

// METHOD SIGNATURE BEGINS, THIS METHOD IS


REQUIRED
public static int lruCountMiss(int max_cache_size, int[] pages) {

int missCount = 0;

if (cache == null) {
cache = new Stack<Integer>();
}

int length = pages.length;


for (int j = 0;j < length; j++){ if(cache.contains(pages[j])){
cache.removeElement(pages[j]);
cache.push(pages[j]);
}else {
if(cache.size() == max_cache_size){
cache.remove(0);
cache.push(pages[j]);
missCount++;
}else if(cache.size() < max_cache_size){
cache.push(pages[j]);
missCount++;
}
}
}

return missCount;
}

public static void main(String[] args) {


int count = lruCountMiss(2,new int[]{2,3,1,3,2,1,4,3,2});
System.out.println(count);
}
}

Вам также может понравиться