- Query string parameters
Pros:
- Very simple to see what values are being passed.
- Built-in .NET capability.
- Useful for bookmark or favorites functionality in an application, because the page can be loaded up using the query string value; not dependent upon data being in a certain state in the application.
- Each browser has a theoretical limit with Internet Explorer being the smallest at 2,048 characters.
- Passing representation of objects is not possible with query strings (i.e. a list of
Customerclass objects). - Represents a potential security risk in that values are exposed. DO NOT pass any sensitive information in a query string.
- In-process session state variables
Pros:
- Built-in .NET capability.
- Allows for storage of large and complex objects, such as list of
Customerobjects. - Resides in memory so you do not need to remember to keep on passing the value between page navigation.
- It is volatile as it is in memory, must keep checking to see if data is actually there or not.
- Can significantly increase the memory usage of your application; this is an issue if you are not the only application on your server.
Monday, October 28, 2013
QueryString Vs Session
Tuesday, July 16, 2013
Refreshing the .NET Application to Read Modified Configuration Files Values
ConfigurationManager.RefreshSection("appSettings");
Tuesday, March 5, 2013
KingdomConnectivity
It has been a prosperous year for King Charles and he is rapidly expanding
his kingdom.A beautiful new kingdom has been recently constructed and in this
kingdom there are many cities connected by a number of one-way roads.
Two cities may be directly connected by more than one roads, this is to
ensure high connectivity.
In this new kingdom King Charles has made one of the cities at his
financial capital and one as warfare capital and he wants high
connectivity between these two capitals.The connectivity of a pair of
cities say city A and city B is defined as the number of different
paths from city A to city B. A path may use a road more than once
if possible. Two paths are considered different if they do not
use exactly the same sequence of roads.
There are N cities numbered 1 to N in the new kingdom and M one-way roads
. City 1 is the monetary capital and city N is the warfare capital.
You being one of the best programmers in new kingdom need to answer the
connectivity of financial capital and warfare capital ,
i.e number of different paths from city 1 to city N.
Input Description:
First line contains two integers N and M.
Then follow M lines ,each having two integers say x and y, 1<=x,y<=N ,
indicating there is a road from city x to city y.
Output Description:
Print the number of different paths from
city 1 to city N modulo 1,000,000,000(10^9).
If there are infinitely many different paths
print "INFINITE PATHS"(quotes are for clarity).
Sample Input:
5 5
1 2
2 4
2 3
3 4
4 5
Sample Output:
2
Sample Input:
5 5
1 2
4 2
2 3
3 4
4 5
Sample Output:
INFINITE PATHS
Constraints:
2<=N<=10,000(10^4)
1<=M<=1,00,000(10^5)
Two cities may be connected by more than two roads and in that case
those roads are to be considered different for counting distinct paths
/*
* KingdomConnectivity.c
* InterviewStreet
*
* Created by Kyle Donnelly on 12/16/12.
*
* Recursively finds the number of paths across a directed graph.
*
* Inputs:
* Number of cities and roads.
* Two integers: city where the road starts and city where it
ends (index 1).
* Two integers: City id (number) which is the monetary capital
* City id (number) which is the war
capital.
* By default these would be 1 and N
for interviewstreet.
*
* Outputs:
* Number of paths from city 1 to city N (modulo MOD_LIMIT).
*
* Base case:
* Current city is the destination, return 1.
*
* Recursive case:
* Current city is not destination
* add up the number of paths of each city this one has access to.
*
* This program forces you to stop once you reach the destination.
*
*/
#include
#include
#include
#define MOD_LIMIT 1000000000
typedef struct {
unsigned int id;
struct _list *children;
unsigned int distance;
} graph;
typedef struct _list {
struct _list *next;
graph *g;
} list;
typedef enum {
UNVISITED, VISITED, LOOPED
} status;
static status *visited; // array of city statuses
static graph **cities; // array of city pointers
static jmp_buf buf; // for loops
/* push a graph into a list of graphs */
void push_graph (list **head, graph *g) {
list *cur;
if (*head == NULL) {
cur = *head = malloc(sizeof(list));
}
else {
for (cur = *head; cur->next != NULL; cur = cur->next);
cur = cur->next = malloc(sizeof(list));
}
cur->g = g;
cur->next = NULL;
}
unsigned int find_paths (graph *source, graph *destination) {
unsigned int retval = 0;
list *cur;
if (source == destination) {
/* Awesome, we made it */
return 1;
}
if (visited[source->id - 1] == VISITED) {
/*
* Loop in graph!
* Not sure if this city actually leads to destination though.
* If not then we don't care.
* Alert earlier paths.
*/
visited[source->id - 1] = LOOPED;
return 0;
}
if (source->distance != -1) {
/* Already know how many from here */
return source->distance;
}
/* Mark that we've gone through this city already */
visited[source->id - 1] = VISITED;
for (cur = source->children; cur != NULL; cur = cur->next) {
/* Check how many paths there are for each child city */
retval = (retval + find_paths(cur->g, destination)) % (MOD_LIMIT);
}
/* Store for future paths through this city */
source->distance = retval;
/* Mark that we're done with this city */
if (visited[source->id - 1] == LOOPED) {
/* One of the paths led back here! */
if (retval > 0) {
/* There is a loop on the way to the destination! */
longjmp(buf, 1);
}
/* But this city didn't lead to the destination anyway */
}
visited[source->id - 1] = UNVISITED;
return (retval);
}
int main() {
unsigned int n, m, to, from, money, war;
unsigned int num_paths;
scanf("%u %u", &n, &m);
cities = malloc(sizeof(graph *) * n);
visited = malloc(sizeof(status) * n);
for (unsigned int i=0; i cities[i] = malloc(sizeof(graph));
cities[i]->id = i+1;
cities[i]->children = NULL;
cities[i]->distance = -1;
visited[i] = UNVISITED;
}
for (unsigned int i=0; i scanf("%u %u", &from, &to);
/* Add node to parent's children */
push_graph(&(cities[from-1]->children), cities[to-1]);
}
scanf("%u %u", &money, &war);
if (!(setjmp(buf))) {
/* Try this */
num_paths = find_paths(cities[money-1], cities[war-1]);
printf("%u\n", num_paths);
}
else {
/* If there are infinite paths just print that */
printf("INFINITE PATHS\n");
}
return 0;
}
StockMaximize
Your algorithms have become so good at predicting the market that you now know what the share price
of Wooden Orange Toothpicks Inc. (WOT) will be for the next N days.
Each day, you can either buy one share of WOT, or sell any number of shares of WOT that you own.
What is the maximum profit you can obtain with an optimum trading strategy?
Input
The first line contains the number of test cases T. T test cases follow:
The first line of each test case contains a number N. The next line contains N integers, denoting the
predicted price of WOT shares for the next N days.
Output
Output T lines, containing the maximum profit which can be obtained for the corresponding test case.
Constraints
1 <= T <= 10
1 <= N <= 50000
All share prices are between 1 and 100000
Sample Input
3
3
5 3 2
3
1 2 100
4
1 3 1 2
Sample Output
0
197
3
Explanation
For the first case, you cannot obtain any profit because the share price never rises.
For the second case, you can buy one share on the first two days, and sell both of them on the third day.
/*
* StockMax.c
* InterviewStreet
*
* Created by Kyle Donnelly on 3/5/13.
*
* Pretty straightforward solution:
* Basically, you want to own as many shares as possible on the day
* that stock is worth the most
* Because any price you could have bought it for previously
* is less than it is now.
* After that day when it's worth the most, just repeat the process.
*
* We keep an array of prices
* and an array of indexes associated with the sorted price array
* Step through those indexes in descending order
* Buy every day since the last peak in price
* simple formula: (this peak index - last peak index) * price of this peak
* then subtract the amount of money it would have cost to buy at all the earlier prices.
*
* Space complexity:
* We need two arrays of length 'n' --> O(n)
*
* Running time complexity:
* Sorting array of length 'n' with quicksort --> O(n*logn)
* Look through each index in decreasing order (look for highest peaks in remaining prediction)
* Step through each price until that peak --> O(n)
* so O(n*logn) overall, sorting is the bottleneck.
*
* Don't have to worry about the order peaks are sorted in if they have the same value
* selling everything or buying that share and selling it later have the exact same effect
*
*/
#include
#include
typedef unsigned int uint;
/*
* Permutes array idx so that the array indexers correspond
* to a sorted order of the array
* but the array is not actually changed.
*/
static void q_sort (uint *arr, uint *idx, uint beg, uint end) {
if (end > beg + 1)
{
uint piv = arr[idx[(end+beg)/2]], l = beg, r = end-1;
while (l <= r)
{
if (arr[idx[l]] > piv)
l++;
else if (arr[idx[r]] < piv)
r--;
else {
uint t = idx[l];
idx[l++] = idx[r];
idx[r--] = t;
}
}
q_sort(arr, idx, beg, r+1);
q_sort(arr, idx, l, end);
}
return;
}
int main (void) {
uint cases, days, *prices, *idx;
uint best_price, last_index;
scanf("%u", &cases);
for (uint i=0; i scanf("%u", &days);
prices = malloc(sizeof(uint) * days);
idx = malloc(sizeof(uint)*days);
best_price = last_index = 0;
for (uint j=0; j scanf("%u", &(prices[j]));
idx[j] = j;
}
q_sort(prices, idx, 0, days);
for (uint j=0; j if (idx[j] >= last_index) {
best_price += prices[idx[j]] * (idx[j] - last_index);
for (uint k=last_index; k best_price -= prices[k];
}
last_index = idx[j] + 1;
}
}
printf("%u\n", best_price);
free(prices);
free(idx);
}
return 0;
}
pairs problem
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PairsDemo
{
class Solution
{
static void Main(string[] args)
{
string input = Console.ReadLine();
int N = Int32.Parse(input.Split(' ')[0]);
long K = long.Parse(input.Split(' ')[1]);
input = Console.ReadLine();
long[] data = new long[N];
string[] ds = input.Split(' ');
for (int i = 0; i K)
{
for (int i = N - 1; i >= 0; i--)
{
for (int j = i - 1; j >= 0; j--)
{
if (data[i] - data[j] == K)
{
result++;
}
else if (data[i] - data[j] > K)
{
break;
}
}
}
}
Console.WriteLine(result);
}
}
}
Flowers problem
Problem:
You and your K-1 friends want to buy N flowers. Flower number i has host ci. Unfortunately the seller does not like a customer to buy a lot of flowers, so he tries to change the price of flowers for customer who had bought flowers before. More precisely if a customer has already bought x flowers, he should pay (x+1)*ci dollars to buy flower number i.
You and your K-1 firends want to buy all N flowers in such a way that you spend the as few money as possible.
Input:
The first line of input contains two integers N and K.
next line contains N positive integers c1,c2,…,cN respectively.
Output:
Print the minimum amount of money you (and your friends) have to pay in order to buy all n flowers.
Sample onput :
3 3
2 5 6
Sample output :
13
Explanation :
In the example each of you and your friends should buy one flower. in this case you have to pay 13 dollars.
Constraint : 1 less than or equal to N,K less than or equal to 100 and each ci is not more than 100,000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FlowersDemo
{
class Solution
{
static void Main(String[] args)
{
int N, K;
string NK = Console.ReadLine();
string[] NandK = NK.Split(new Char[] { ' ', '\t', '\n' });
N = Convert.ToInt32(NandK[0]);
K = Convert.ToInt32(NandK[1]);
int[] C = new int[N];
string numbers = Console.ReadLine();
string[] split = numbers.Split(new Char[] { ' ', '\t', '\n' });
double result = 0;
int i = 0;
foreach (string s in split)
{
if (s.Trim() != "")
{
C[i++] = Convert.ToInt32(s);
if (N == K)
result += Convert.ToInt32(s);
}
}
if (N != K)
{
Array.Sort(C);
int counter = 0;
for (int a = N - 1; a >= 0; a--)
{
result += Math.Ceiling((double)(counter/K)+1) * C[a];
counter++;
}
}
Console.WriteLine((int)result);
}
}
}
You and your K-1 friends want to buy N flowers. Flower number i has host ci. Unfortunately the seller does not like a customer to buy a lot of flowers, so he tries to change the price of flowers for customer who had bought flowers before. More precisely if a customer has already bought x flowers, he should pay (x+1)*ci dollars to buy flower number i.
You and your K-1 firends want to buy all N flowers in such a way that you spend the as few money as possible.
Input:
The first line of input contains two integers N and K.
next line contains N positive integers c1,c2,…,cN respectively.
Output:
Print the minimum amount of money you (and your friends) have to pay in order to buy all n flowers.
Sample onput :
3 3
2 5 6
Sample output :
13
Explanation :
In the example each of you and your friends should buy one flower. in this case you have to pay 13 dollars.
Constraint : 1 less than or equal to N,K less than or equal to 100 and each ci is not more than 100,000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FlowersDemo
{
class Solution
{
static void Main(String[] args)
{
int N, K;
string NK = Console.ReadLine();
string[] NandK = NK.Split(new Char[] { ' ', '\t', '\n' });
N = Convert.ToInt32(NandK[0]);
K = Convert.ToInt32(NandK[1]);
int[] C = new int[N];
string numbers = Console.ReadLine();
string[] split = numbers.Split(new Char[] { ' ', '\t', '\n' });
double result = 0;
int i = 0;
foreach (string s in split)
{
if (s.Trim() != "")
{
C[i++] = Convert.ToInt32(s);
if (N == K)
result += Convert.ToInt32(s);
}
}
if (N != K)
{
Array.Sort(C);
int counter = 0;
for (int a = N - 1; a >= 0; a--)
{
result += Math.Ceiling((double)(counter/K)+1) * C[a];
counter++;
}
}
Console.WriteLine((int)result);
}
}
}
candies problem
Problem:
Alice is a kindergarden teacher. She wants to give some candies to the children in her class. All the children sit in a line and each of them has a rating score according to his or her usual performance. Alice wants to give at least 1 candy for each child.Children get jealous of their immediate neighbors, so if two children sit next to each other then the one with the higher rating must get more candies. Alice wants to save money, so she wants to minimize the total number of candies.
Input
The first line of the input is an integer N, the number of children in Alice’s class. Each of the following N lines contains an integer indicates the rating of each child.
Ouput
Output a single line containing the minimum number of candies Alice must give.
Sample Input
3
1
2
2
Sample Ouput
4
Explanation
The number of candies Alice must give are 1, 2 and 1.
Constraints:
N and the rating of each child are no larger than 10^5.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CandiesDemo
{
class Solution
{
static void Main(string[] args)
{
int N = Int32.Parse(Console.ReadLine());
int[] ranks=new int[N] ;
int[] candi = new int[N];
int results = 0;
for (int i = 0; i < N; i++)
{
ranks[i] = Int32.Parse(Console.ReadLine());
candi[i] = 1;
}
bool IsCompleted = false;
while (!IsCompleted)
{
IsCompleted = true;
for (int i = 0; i < N; i++)
{
if (i == 0)
{
if (ranks[0] > ranks[1] && candi[0] <= candi[1])
{
candi[0] = candi[1] + 1;
IsCompleted = false;
}
}
else if (i == N - 1)
{
if (ranks[N - 1] > ranks[N - 2] && candi[N - 1] <= candi[N - 2])
{
candi[N - 1] = candi[N - 2] + 1;
IsCompleted = false;
}
}
else
{
if (ranks[i] > ranks[i - 1] && candi[i] <= candi[i - 1])
{
candi[i] = candi[i - 1] + 1;
IsCompleted = false;
}
else if (ranks[i] > ranks[i + 1] && candi[i] <= candi[i + 1])
{
candi[i] = candi[i + 1] + 1;
IsCompleted = false;
}
}
}
}
for (int i = 0; i < N; i++)
{
results += candi[i];
}
Console.WriteLine(results);
}
}
}
Alice is a kindergarden teacher. She wants to give some candies to the children in her class. All the children sit in a line and each of them has a rating score according to his or her usual performance. Alice wants to give at least 1 candy for each child.Children get jealous of their immediate neighbors, so if two children sit next to each other then the one with the higher rating must get more candies. Alice wants to save money, so she wants to minimize the total number of candies.
Input
The first line of the input is an integer N, the number of children in Alice’s class. Each of the following N lines contains an integer indicates the rating of each child.
Ouput
Output a single line containing the minimum number of candies Alice must give.
Sample Input
3
1
2
2
Sample Ouput
4
Explanation
The number of candies Alice must give are 1, 2 and 1.
Constraints:
N and the rating of each child are no larger than 10^5.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CandiesDemo
{
class Solution
{
static void Main(string[] args)
{
int N = Int32.Parse(Console.ReadLine());
int[] ranks=new int[N] ;
int[] candi = new int[N];
int results = 0;
for (int i = 0; i < N; i++)
{
ranks[i] = Int32.Parse(Console.ReadLine());
candi[i] = 1;
}
bool IsCompleted = false;
while (!IsCompleted)
{
IsCompleted = true;
for (int i = 0; i < N; i++)
{
if (i == 0)
{
if (ranks[0] > ranks[1] && candi[0] <= candi[1])
{
candi[0] = candi[1] + 1;
IsCompleted = false;
}
}
else if (i == N - 1)
{
if (ranks[N - 1] > ranks[N - 2] && candi[N - 1] <= candi[N - 2])
{
candi[N - 1] = candi[N - 2] + 1;
IsCompleted = false;
}
}
else
{
if (ranks[i] > ranks[i - 1] && candi[i] <= candi[i - 1])
{
candi[i] = candi[i - 1] + 1;
IsCompleted = false;
}
else if (ranks[i] > ranks[i + 1] && candi[i] <= candi[i + 1])
{
candi[i] = candi[i + 1] + 1;
IsCompleted = false;
}
}
}
}
for (int i = 0; i < N; i++)
{
results += candi[i];
}
Console.WriteLine(results);
}
}
}
Subscribe to:
Posts (Atom)